String Formatting

# Define string variable 
string1 = "I am a string."
string2 = 'I am a string too.'

# Multi-line strings 
string3 = """ I am a long 
long 
string."""

# String containing a quote 
string4 = "I\'m a string."

# Print new line character 
string5 = "I\'m a string. \n I\'m on a new line."

# Multiply string lenth 
string6 = "a" * 10

# Get length of a string 
print(len(string6))

# Check for items within strings 
print("string" in string4)

# Check if string starts with a certain value 
print(string4.startswith("I"))
print(string4.startswith("n"))

# Check index of string 
print(string4.index("string"))

# Transform to upper or lowercase 
print(string4.upper())
print(string4.lower())

# Cleanup messy strings 
messy_string = "          Messy string        "
print(messy_string.stip())

# Replace values in string 
print(messy_string.replace("!","?"))

# Replace and cleanup string 
print(messy_string.replace("!","?").strip())

# Split into two strings 
print(messy_string.split())

# Split into two strings at a specific character 
print(messy_string.split(","))

# Specify encoding 
print(string4.encode())
print(string4.encode("utf-8"))

# Right and left justify 
print(string4.rjust(25))
print(string4.ljust(25))
print(string4.rjust(25,"X"))

Last updated