0

I am taking an input string that is all one continuous group of letters and splitting it into a sentence. The problem is that as a beginner I can't figure out how to modify the string to ONLY capitalize the first letter and convert the others to lowercase. I know the string.lower but that converts everything to lowercase. Any ideas?

 # This program asks user for a string run together
 # with each word capitalized and gives back the words
 # separated and only the first word capitalized
 import re
 def main():
      # ask the user for a string
      string = input( 'Enter some words each one capitalized, run together without spaces ')
      for ch in string:
         if ch.isupper() and not ch.islower():
            newstr = re.sub('[A-Z]',addspace,string)
      print(newstr)
 def addspace(m) :
      return ' ' + m.group(0)
 #call the main function
 main()
2
  • 1
    I expanded my answer to capitalize only the first word and lower case all other words. Commented Apr 6, 2014 at 3:45
  • first word or first char of the first word ? Commented Apr 6, 2014 at 3:46

3 Answers 3

4

You can use capitalize():

Return a copy of the string with its first character capitalized and the rest lowercased.

>>> s = "hello world"
>>> s.capitalize()
'Hello world'
>>> s = "hello World"
>>> s.capitalize()
'Hello world'
>>> s = "hELLO WORLD"
>>> s.capitalize()
'Hello world'
Sign up to request clarification or add additional context in comments.

2 Comments

The first word in the string is already capitalized. I want all the other words in the string the be uncapitalized
@Maggi3339336 then, you better still use capitalize() - it's clear and readable.
0

Unrelated example. To capitalize only the first letter you can do:

>>> s = 'hello'
>>> s = s[0].upper()+s[1:]
>>> print s
Hello

>>> s = 'heLLO'
>>> s = s[0].upper()+s[1:]
>>> print s
HeLLO

For a whole string, you can do

>>> s = 'what is your name'
>>> print ' '.join(i[0].upper()+i[1:] for i in s.split())
What Is Your Name

[EDIT]

You can also do:

>>> s = 'Hello What Is Your Name'
>>> s = ''.join(j.lower() if i>0 else j for i,j in enumerate(s))
>>> print s
Hello what is your name

3 Comments

why you don't use just str.capitalize() ?
@sjcipher, Because alecxe did that. It is better we provide a range of options to choose from rather than all putting the same best answer.
ok thank you, its good to know all those option. appreciate that.
0

If you only want to capitalize the start of sentences (and your string has multiple sentences), you can do something like:

>>> sentences = "this is sentence one. this is sentence two. and SENTENCE three."
>>> split_sentences = sentences.split('.')
>>> '. '.join([s.strip().capitalize() for s in split_sentences])
'This is sentence one. This is sentence two. And sentence three.  '

If you don't want to change the case of the letters that don't start the sentence, then you can define your own capitalize function:

>>> def my_capitalize(s):
        if s:     # check that s is not ''
            return s[0].upper() + s[1:]
        return s

and then:

>>> '. '.join([my_capitalize(s.strip()) for s in split_sentences])
'This is sentence one. This is sentence two. And SENTENCE three. '

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.