Conversion and Comparison
Learn about string conversion and comparison in Python.
We'll cover the following...
String conversions
Two types of string conversions are frequently required:
- Converting the case of characters in a string.
- Converting numbers to a string and vice versa.
Case conversions
Case conversions can be done using the following str
methods:
upper()
: Converts string to uppercase.lower()
: Converts string to lowercase.capitalize()
: Converts the first character of a string to uppercase.title()
: Converts the first character of each word to uppercase.swapcase()
: Swaps cases in the string.
Press + to interact
msg = 'Hello'print(msg.upper( )) # prints HELLOprint('Hello'.upper( )) # prints HELLOs1 = 'Bring It On'# Conversionsprint(s1.upper( ))print(s1.lower( ))print(s1.capitalize( ))print(s1.title( ))print(s1.swapcase( ))