Table of Contents
In Python, the capitalize() method converts first character of a string to uppercase letter and lowercases all other characters, if any.
The syntax of capitalize()
is:
string.capitalize()
1. capitalize() Parameter
The capitalize()
function doesn’t take any parameter.
2. Return Value from capitalize()
The capitalize()
function returns a string with the first letter capitalized and all other characters lowercased. It doesn’t modify the original string.
3. Example 1: Capitalize a Sentence
string = "python is AWesome." capitalized_string = string.capitalize() print('Old String: ', string) print('Capitalized String:', capitalized_string)
Output
Old String: python is AWesome Capitalized String: Python is awesome
4. Example 2: Non-alphabetic First Character
string = "+ is an operator." new_string = string.capitalize() print('Old String:', string) print('New String:', new_string)
Output
Old String: + is an operator. New String: + is an operator.
Related posts:
Python Numbers, Type Conversion and Mathematics
Python Program to Print Output Without a Newline
Python Decorators
Python Program to Catch Multiple Exceptions in One Line
Python Program to Find ASCII Value of Character
Python List append()
Python min()
Python format()
JavaScript Methods of RegExp and String
Python Recursion
Python filter()
Python String splitlines()
Python String isalnum()
Java InputStream to String
Python Program to Copy a File
Python List reverse()
Python Tuple count()
Python dict()
Python range()
Python Program to Concatenate Two Lists
String Set Queries
Python Program to Convert Kilometers to Miles
Python Program to Print all Prime Numbers in an Interval
Python String strip()
Python Program to Check if a Number is Positive, Negative or 0
Python Program to Check if a Key is Already Present in a Dictionary
Python String endswith()
Python Object Oriented Programming
Python Set update()
Python Set intersection()
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python String title()