The .title() method of a string (either ASCII or Unicode is fine) does this:
>>> "hello world".title()
'Hello World'
>>> u"hello world".title()
u'Hello World'
However, look out for strings with embedded apostrophes, as noted in the docs.
Answer from Mark Rushakoff on Stack OverflowThe algorithm uses a simple language-independent definition of a word as groups of consecutive letters. The definition works in many contexts but it means that apostrophes in contractions and possessives form word boundaries, which may not be the desired result:
>>> "they're bill's friends from the UK".title() "They'Re Bill'S Friends From The Uk"
The .title() method of a string (either ASCII or Unicode is fine) does this:
>>> "hello world".title()
'Hello World'
>>> u"hello world".title()
u'Hello World'
However, look out for strings with embedded apostrophes, as noted in the docs.
The algorithm uses a simple language-independent definition of a word as groups of consecutive letters. The definition works in many contexts but it means that apostrophes in contractions and possessives form word boundaries, which may not be the desired result:
>>> "they're bill's friends from the UK".title() "They'Re Bill'S Friends From The Uk"
The .title() method can't work well,
>>> "they're bill's friends from the UK".title()
"They'Re Bill'S Friends From The Uk"
Try string.capwords() method,
import string
string.capwords("they're bill's friends from the UK")
>>>"They're Bill's Friends From The Uk"
From the Python documentation on capwords:
Split the argument into words using str.split(), capitalize each word using str.capitalize(), and join the capitalized words using str.join(). If the optional second argument sep is absent or None, runs of whitespace characters are replaced by a single space and leading and trailing whitespace are removed, otherwise sep is used to split and join the words.
python - how to change the case of first letter of a string? - Stack Overflow
How do I capitalize the first letter of a string, but have it respect forced capitalizations anyway
Changing the first letter of a string into upper case in python - Stack Overflow
python capitalize first letter only - Stack Overflow
Both .capitalize() and .title(), changes the other letters in the string to lower case.
Here is a simple function that only changes the first letter to upper case, and leaves the rest unchanged.
def upcase_first_letter(s):
return s[0].upper() + s[1:]
You can use the capitalize() method:
s = ['my', 'name']
s = [item.capitalize() for item in s]
print s # print(s) in Python 3
This will print:
['My', 'Name']
For example, "cotton tails" will be "Cotton Tails"
However, if it's COTTON TAILS, it should still be COTTON TAILS
I tried .title() but that capitalizes the first letter of each word but automatically sets the rest of the characters to lower case.
So "COTTON TAILS" would be "Cotton Tails" and I do not want that. I want every first letter of each word to be capitalized, but any other hard coded capitalizations should be retained.
ED
Thanks. for the quick replies. I will make a function for this.
Only because no one else has mentioned it:
>>> 'bob'.title()
'Bob'
>>> 'sandy'.title()
'Sandy'
>>> '1bob'.title()
'1Bob'
>>> '1sandy'.title()
'1Sandy'
However, this would also give
>>> '1bob sandy'.title()
'1Bob Sandy'
>>> '1JoeBob'.title()
'1Joebob'
i.e. it doesn't just capitalize the first alphabetic character. But then .capitalize() has the same issue, at least in that 'joe Bob'.capitalize() == 'Joe bob', so meh.
If the first character is an integer, it will not capitalize the first letter.
>>> '2s'.capitalize()
'2s'
If you want the functionality, strip off the digits, you can use '2'.isdigit() to check for each character.
>>> s = '123sa'
>>> for i, c in enumerate(s):
... if not c.isdigit():
... break
...
>>> s[:i] + s[i:].capitalize()
'123Sa'
Use the capitalize string method on each word in the list and then join the strings together with an underscore:
>>> '_'.join(x.capitalize() for x in s)
'Smith_Jones_Paul'
In summary...
capitalizeputs a string's first character to uppercase and the subsequent characters to lowercase.(x.capitalize() for x in s)applies thecapitalizeto each string in your lists.jointakes a list (or other iterable) of strings and joins the strings together with a delimiter (in this case an underscore).
N.B. It's worth noting that passing a list to join is more efficient than giving it the generator expression shown above:
>>> %timeit '_'.join([x.capitalize() for x in s]) # list
1000000 loops, best of 3: 866 ns per loop
>>> %timeit '_'.join(x.capitalize() for x in s) # generator
100000 loops, best of 3: 2.05 us per loop
Actually, doing the "manual" work is (just a little) faster:
>>> "_".join([x[0].upper() + x[1:] for x in s])
'Smith_Jones_Paul'
Actually a bit confused with the benchmark:
>>> %timeit '_'.join([x.capitalize() for x in s])
1000000 loops, best of 3: 817 ns per loop
>>> %timeit '_'.join([x[0].upper() + x[1:] for x in s])
1000000 loops, best of 3: 796 ns per loop
The variation is really high in this case. But when I do a simple for loop for this case:
z = ["aasdfasdfasdfsadfa", "aasdfasdfasdfsadfa", "aasdfasdfasdfsadfa",
"aasdfasdfasdfsadfa", "aasdfasdfasdfsadfb"] * 10
for i in range(1000000):
a = [x[0] + x[1:] for x in z]
for i in range(1000000):
b = [x.capitalize() for x in z]
There is a difference of 9 vs 16 seconds!
Just use str.title():
In [73]: a, b = "italic","ITALIC"
In [74]: a.title(), b.title()
Out[74]: ('Italic', 'Italic')
help() on str.title():
S.title() -> string
Return a titlecased version of S, i.e. words start with uppercase
characters, all remaining cased characters have lowercase.
Yeah, just use the capitalize() method.
eg:
x = "hello"
x.capitalize()
print x #prints Hello
Title will actually capitalize every word as if it were a title. Capitalize will only capitalize the first letter in a string.