...

/

Conversion and Comparison

Conversion and Comparison

Learn about string conversion and comparison in Python.

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 HELLO
print('Hello'.upper( )) # prints HELLO
s1 = 'Bring It On'
# Conversions
print(s1.upper( ))
print(s1.lower( ))
print(s1.capitalize( ))
print(s1.title( ))
print(s1.swapcase( ))

String to number conversions

...