Python case string

Python offers 4 methods that are useful to change the case of a string. They are upper(), lower(), swapcase(), title() . The upper() method is used to convert all the characters of a string into uppercase or capital letters. The lower() method converts the string into lowercase or into small letters. The swapcase() method converts the capital letters into small letters and vice versa. The title() method converts the string such that each word in the string will start with a capital letter and remaining will be small letters.

CopiedCopy Code

str = 'Python is future' 
print(str.lower())
print(str.upper())
print(str.swapcase())
print(str.title())

Checking Starting and Ending of a String

The startswith() method is useful to know whether a string is starting with a sub string or not. The way to use this method is:

str.startswith(substring)

When the sub string is found in the main string 'str', this method returns True. If the sub string is not found, it returns False. Consider the following statements:

CopiedCopy Code

str = 'This is Python' 
print(str.startswith('This'))

Similarly, to check the ending of a string, we can use endswith() method. It returns True if the string ends with the specified sub string, otherwise it returns False.

CopiedCopy Code

str.endswith(substring) 
str = 'This is Python' 
print(str.endswith('Python'))