String Manipulation in Python

Beginners code Computer Skills Mobile Development Python Web Development

String manipulation in Python

String manipulation in python plays a major role to deal with strings in various ways and thus allows us to perform various types of operations with inputted strings.

  • Strings in python are an array of characters like any other programming languages.
  • By string manipulation we mean it can store any form of character within the double quotes. It also accepts strings within single quotes, but we need to maintain uniformity in basic coding pattern. So we would be sticking to what we have learned from previous coding languages.
  • By all the characters we mean it also includes spaces as a type of string too. Thus, whatever we write in between double quotes, python will treat it as a string only.
  • Python strings are immutable. It means, once we create the string, we cannot modify its values further in a program.

Operations on Strings

Once a string is created, we can have a lot of operations to perform on it. The most basic string manipulation operations include printing them in a proper format while the advanced ones are slicing the texts and checking for various patterns of characters.

Python supports a wide range of functions to work on string manipulation. Most of these functions are very easy to use. Unlike languages like Java, we do not have to write several lines of code in performing essential operations. Not even for the advanced operations! Yes. You get to work on all these features with most of them provided as an inbuilt feature.

Creating and Accessing String

The following code shows some basic methods of extracting character set from an array of string.

word = "My name is Mario. I love Mushrooms"
print(word)                    # prints the entire input in the given format
print(word[5])                 # prints the alphabet at index location 5
print(word[5:19])              # prints the sequence of characters from index location 5 to 19
print(len(word))               # prints the total number of characters in the given input string

Counting Occurrence and Searching Characters

  • We can count the number of occurrence of specific characters in python.
  • Note that the input for this function is case-sensitive i.e. It treats upper-case and lower-case alphabets differently.
  • Here are a few examples to try on:
word = "My name is Mario. I love Mushrooms"
print(word.count("M"))
print(word.find("v"))
print(word.index("Mushrooms"))

Try and run the program. Let us know what did you conclude from the output.

NOTE: The index function simply allows us to determine where a particular word begins.

Character Slicing

  • Character slicing refers to extracting a part of the character from a given string. Extracting parts of string can be highly useful in cases where partial analysis of information has to be done.
  • This method is very helpful in advanced studies like Sentiment analysis of tweets or Facebook posts.
  • Here, we extract the sentimental words from the post and determine the mood of people.
  • The basic format is as follows:
word = "My name is Mario. I love Mushrooms"
print(word[:-3])      # omits the last three characters
print(word[-3:])      # prints the last three characters
print(word[:3])       # omits the first three characters
print(word[3:])       # prints the first three characters

Checking and Replacing Functions:

  • Python supports functions which can be used to check whether a particular character sequence is present in the given string input.
  • We can obtain Boolean results for them and at the same time, we can also use replacing functions to change a particular sequence of characters.
word = "My name is Mario. I love Mushrooms"
# Checking functions
print(word.startswith('My'))
print(word.endswith("rooms"))
# Replacing Characters
print(word.replace("love", "eat"))

 

NOTE: Most of you must have thought how is replacing possible if strings are immutable!

We are not replacing the actual input. It remains as it is. We are using only certain manipulation functions to generate new results. This effect is not permanently stored. If you assign this changed string value to the working variable then the effect is permanently stored.

This situation shows that the variable’s value itself is completely changed rather than modifying a part of it. Thus in no case, Strings are mutable.

Changing Cases and Splitting Strings:

  • Python allows us to change cases of a given string by using its predefined functions.
  • Some common functions include upper(), lower() and swapcase().
  • The splitting of characters helps us to segregate each sequence of a given string and thus tokenize them for further processing.
  • The format of changing cases and Splitting them is as follows:
word = "My name is Mario. I love Mushrooms"

# change cases of string
print(word.upper())
print(word.lower())
print(word.swapcase())

# Split Strings
print(word.split(' '))

Try and run this code. Let us know what outputs did you all receive! Can you tell the form in which received the split words?

Miscellaneous functions for String Manipulation:

As you go on delving deeper into Python, you would realize countless functions to work on. Even to deal with strings, there are a lot of functions which can ease our work in a few lines instead of writing multiple lines of code.

Few interesting String functions are given below:

  • Stripping Functions: 
    • They trim the spaces enclosed on either side of the given string input and re-enclose them in the double quotes.
    • It goes as follows:
word = "           My name is Mario. I love Mushrooms          "

# Stripping string inputs
print(word.lstrip())       # Strips the spaces to the left of input string
print(word.rstrip())       # Strips the spaces to the right of input string
  • Miscellaneous Functions: Few more sample functions include-
word = "My name is Mario. I love Mushrooms"
print(word.isalpha())       # checks whether it has alphabets or not
print(word.isdigit())       # checks whether it has any digit or not
print(word.isspace())       # checks whether it has any space or not

The above 3 functions work as:

  • isalpha(): It checks whether the inputted string has alphabets or not.
  • isdigit(): It checks whether the inputted string has any digits or not.
  • isspace(): checks whether the inputted string has any space or not.