Search⌘ K

Conversion and Comparison

Explore methods for converting strings to different cases and numeric types in Python. Understand how to compare strings lexicographically and work with byte sequences for binary data representation.

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.
Python 3.8
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

...