Join us

String Manipulation in Python: A Comprehensive Guide

1_dwVL-apx5IpEtuHyz9vLCw.jpeg

As a software developer, you will be dealing with strings quite regularly.

It is important you master all the tricks you can apply to them. This is why string manipulation is tested in coding interviews.

We will evaluate Python functions and methods that allow you to manipulate strings.

Escape Characters

These are characters in Python represented with backslash (\).
Backslash n also expressed as (\n) represents a new line.

Backslash t also represented as (\t) represents tab, backslash backslash (\\) is the actual backslash character in strings.

Example:

We have the variable “words” and it contains the string, “Let us master Python strings”. We want stings to fall into a new line and we will use (\n) to tell the interpreter to take strings to a line of its own.

Let us test the tab escape character;

Raw Strings

Our next useful function for strings is the raw string function, that allows us to include nested characters such as “” and \.

If we want to ignore the escape characters (\) and “” we can use the regex method, the r will convert our entire string to a normal string

r = represents regex characters, which is a method that converts our entire sentence into normal characters, so let’s say we put it in the sentence we put the r in the sentence of (/n) then the escape character will be ignored.

Example:

As you can notice above the \t does not play the role of escape character, it is a normal character in the string.

In and Not in

in and not in operation in strings, if we have a string “Facebook was down” and we want to check if a specific word exists in the sentence.

Example:

“Facebook” in words this would return True
“Twitter” in words we will get false

We can also use a not in function to check if it does not exist, i.e. “Facebook” not in words, we will get False as it does exist.

So, in and not in are two functions you can use to see if some specific string or word exists in a bunch of texts.

Upper, Lower, isUpper, isLower

Example:

We have the string “I love blue coding”

We want to convert this sentence to all uppercase, so we can use the Python function words.upper() to convert our entire text to all caps lock and words.lower() to convert it all to lower case.

To check if the text is upper case or lower case, we can use the isupper() or islower().

words.isupper()

or

words.islower()

So upper and lower are two functions you can use to uppercase or lowercase your strings simultaneously.

Is Function

The is function is used to see if statements are True or False, so the function returns True or False

isnumeric()

We want to know if words.isnumeric() is true or false.

We also want to know if words.isnumeric() is true or false

Interestingly we can also use the .isdigit() in this case.

isdecimal()

We want to know if words.isdecimal() is true or false.

We also want to know if words.isdecimal() is true or false.

Starts With and End With

Starts with function is something we can apply to strings to check if a string starts with or ends with a certain word.

We want to check if “We” is the first letter of the sentence.

When we want to check if the sentence ends with “task”.

These are very fun functions we can use to check the beginning and the ending of your sentences.

Split Function

Whenever we have a sentence, let’s say it is one giant sentence, and we want every individual word and we want to apply some other function to that individual word and we want to split that sentence by something.

We want to split this and perform functions on each character in the string. Using the split function, we can go ahead and split by space, the split function takes in the delimiter by which you want to split up every single word or every single item inside of your string. So by splitting it by space every single time the Python interpreter encounters a space, it will go ahead and remove it and store it as an array.

We get every single character in the string

Split and Join function

Similarly, for when we want to join our sentence. The split function has split up our entire sentence into five different words.

Let’s store it

Then we want to join our sentence splitUpSentence.join but we have to join with a specific keyword

What the join did was add a space in the split sentence.

The split function is used to split up the string based on a specific delimiter and the join function is used to append something to every single item in your list.

The split function is very useful when dealing with CSV files.

Example:

Strip Function

The strip function removes all of the excess space that we do not want.
We can also specify where we want to strip that extra text from, we can strip left or right.

We can use sent.lstrip() to strip left, .lstrip strips all the extra whitespace on the left.

We can use sent.rstrip() to strip right, rstrip strips all the extra whitespace on the right.

And lastly, .strip can be used to strip extra whitespace on both sides. So essentially, the strip function is used to remove all the extra whitespace we might have.

Example:

All the excess space is removed

Let’s say we accidentally split the data

And there are whitespaces before the cherry, then we can use the strip function to get rid of the whitespaces.


Only registered users can post comments. Please, login or signup.

Start blogging about your favorite technologies, reach more readers and earn rewards!

Join other developers and claim your FAUN account now!

Avatar

The Maths Geek 🤓

@thenjikubheka
Mathematician | Software Engineer | Amazonian| Open Source | Blogger | Ban Killer Robots
User Popularity
343

Influence

34k

Total Hits

22

Posts

Mentioned tools