Let us continue with Python Tutorials for Beginners. If you are new to Python strings, please see my Python tutorial 2 | Handling Strings first. This Python beginner tutorial explains many useful Python string functions with examples. Please view the Python video tutorial 9 or read on...
Now let us see Python examples of string methods in Python programming.
# Python code example
greeting = "Hello"
# print in lower case
print (greeting.lower())
# print in upper case
print (greeting.upper())
name = 'john doe'
# print only the first character in capitals with capitalize
print (name.capitalize())
# print the first character capitalized in every word with title
print (name.title())
# Python code example
some_string = " this is some text "
# strip the left hand side spaces with lstrip
print (some_string.lstrip())
# strip the given left hand side characters i.e. spaces, t and h with lstrip
print (some_string.lstrip(' th'))
# strip the right hand side spaces with rstrip
print (some_string.rstrip())
# strip the given right hand side characters i.e. t and spaces with rstrip
print (some_string.rstrip('t '))
# strip spaces on both ends with strip
print (some_string.strip())
Now let us see Python examples of string methods in Python programming.
# Python code example
greeting = "Hello"
# print in lower case
print (greeting.lower())
# print in upper case
print (greeting.upper())
name = 'john doe'
# print only the first character in capitals with capitalize
print (name.capitalize())
# print the first character capitalized in every word with title
print (name.title())
# Python code example
some_string = " this is some text "
# strip the left hand side spaces with lstrip
print (some_string.lstrip())
# strip the given left hand side characters i.e. spaces, t and h with lstrip
print (some_string.lstrip(' th'))
# strip the right hand side spaces with rstrip
print (some_string.rstrip())
# strip the given right hand side characters i.e. t and spaces with rstrip
print (some_string.rstrip('t '))
# strip spaces on both ends with strip
print (some_string.strip())