Python String capitalize() method
The string.capitalize() method in Python returns a copy of the string with only its first character capitalized.
Syntax
1 2 3 |
string.capitalize() |
Parameter
NA
Return
This function returns a string where the first character is upper case.
Example 1
1 2 3 4 5 6 7 8 9 |
# Python String program explaining # the capitalize() method # passing the string value strVal = "hello world"; print("String value:",strVal) # capitalizing the first character of the String print ("After capitalizing the string:", strVal.capitalize()) |
Output
1 2 3 4 |
String value: hello world After capitalizing the string: Hello world |
Example 2
1 2 3 4 5 6 7 8 9 |
# Python String program explaining # the capitalize() method #passing the first character as integer str = "21 is my roll no." # it will return a copy of the string as the first digit is integer n = str.capitalize() print (n) |
Output
1 2 3 |
21 is my roll no. |
Example 3
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# Python program to demonstrate the # use of capitalize() function # passing the string value list = "the list of participants are as follow:" print(list.capitalize()) # capitalization of individual words to generate camel case name1 = "reema," name2 = "amar," name3 = "varun," name4 = "sukla" print(name1.capitalize() + name2.capitalize() + name3.capitalize()+name4.capitalize()) |
Output
1 2 3 4 |
The lists of participants are as follow: Reema,Amar,Varun,Sukla |