Common String Operations in Python

In the previous article we have seen some of the list operations, along with string operations. Now we will look at some more vital string operations, keywords or functions in Python.

Transformation/Manipulation of Strings

There are a number of fun methods that we can use to transform our string text. Among those more important for building real-world applications are the lower () , upper() , strip (), replace (), and join () methods .

The strip () method is useful when processing user input because it removes surrounding spaces in the string. This means that not only spaces are removed, but tabs and new line characters are also removed. These are all characters that we normally don’t need in user-supplied strings. There are two other versions of this method, lstrip and rstrip , which remove spaces to the left or right of the string instead of both sides.

#Specifies to remove space from ends of string
a = "  Hello, World!  "
print(a.strip())
Output:
Hello, World!

The use of the function .strip() removes the space at the end of the statement.

The orc() function returns the UNICODE numeric value of the string characters.

#Print out  the unicode values of 'a', 'A', and '#'
print(ord('a'))
print(ord('A'))
print(ord('#'))
Output:
97
65
35

Upper and Lower Case Strings

Two commonly used string functions in Python are the ones which us to change the cases of your strings to upper or lower case. The functions upper() and lower() will return a string with all the original letters in the string converted into either upper or lower case.

#String statement to be converted 
string_x = "String Statement"

#Specifies to convert string elements to uppercase
print(string_example.upper())
#Specifies to convert string elements to lowercase
print(string_example.lower())
Output:
STRING STATEMENT
string statement

Additionally, we use the capitalize()  to capitalize the first character of a string, and the title()  to capitalize the first letter of every word in a string.

Boolean String Methods

Boolean string methods that return yes or no depending on the condition of the string.

  • str.isupper(): Checks if the string’s alphabetic characters are all uppercase
  • str.islower(): Checks if the String’s alphabetic characters are all lower case
  • str.istitle(): Checks if the String is in title case
  • str.isspace(): Checks if the String only consists of whitespace characters
  • str.isnumeric(): Checks if the String only contains numeric characters
  • str.isalnum(): Checks if the String contains only alphanumeric characters (no symbols)
  • str.isalpha(): Checks if the String contains only alphabetic characters (no numbers or symbols)
#Checks if the string's alphabetic characters are all uppercase
string_example = "String Example"
print(string_example.upper())

#Checks if the String’s alphabetic characters are all lower case
str = "THIS is string example....wow!!!"; 
print str.islower()

#Checks if the String is in title case
sentence = "This is a sentence."
print(sentence.istitle())

#Checks if the String only consists of whitespace characters
txtspace = "   "
print(txtspace.isspace())

#Checks if the String only contains numeric characters
s = '1242323'
print(s.isnumeric())

#Checks if the String contains only alphanumeric characters
namex = "E234lizabeth"
print(namex.isalnum())

#Checks if the String contains only alphabetic characters
name = "Elizabeth"
print(name.isalpha())
Output:
False
False
False
True
True
True
True

join() method

The join() method reads all the elements in an iterable data type(tuples, lists etc) and converts them into a string. A string must be specified as the separator.

#Tuple elements to be joined together into a string
myTuple = ("Peter", "Tim", "Martin")

#Statement to join the elements and print them out
print("#".join(myTuple))
Output:
Peter#Tim#Martin

replace() method

It replaces the matching characters or segment portion of a string with the specified characters given by the user.

#String with words to be replaced using replace() method
txt = "Long time ago, when the sky was still...."

#Specifies to replace the characters "Long" with "Short"
print(txt.replace("Long", "Short"))
Output:
Short time ago, when the sky was still....

Leave a Comment