How to work with and modify texts (strings) in Python?
top of page

How to work with and modify texts (strings) in Python?


Python text join text, chr index, split text, trim text, remove whitespace, find, filter, count, isnumeric, isalpha, isalnum, left, mid, split, replace, len, convert to lower case or upper case

Strings is just another word for text and in order to write strings you need to use the " quotation signs or ' apostrophe signs. This part will go through some practical ways to work with and modify strings to your specification. We will start to work with the classic analyst phrase. "Please Fix Thx." and modify that one throughout the examples on this page. Let's start with adding the text parts in to three strings.

# Text variables.
A = "Please"
B = "Fix"
C = "Thx."

# Alternatively with apostrophes..
A = 'Please'
B = 'Fix'
C = 'Thx.'

Below are some examples of your normal text operations


Join strings

We can combine or join strings with the and '+' operator where we can mix variables and texts.


'No spaces
print (A + B + C) --> "PleaseFixThx."
 
'With spaces
print (A + " " + B + " " + C) --> "Please Fix Thx."

Now Let's add them all into one string D for further modifications.

'Let's make sure these are added into one string D.
D = A + " " + B + " " + C

Left Syntax


#Now let's take out the first part ("Please") from D.
# Every character (chr) is considered 1 item in an array of letters meaning we can just index our chr we want from our text string "array"

D[:6] --> Please

Right Syntax


#Let's take out the last part ("Thx.")
D[-4:] --> Thx.

Mid Syntax

# To isolate the middle part in the text we need to consider that the starting position in python is 0 here.
#  same as Mid(D, 8, 3) in excel.
D[7:10] --> Fix

Split Syntax

Split is a practical syntax for dividing a string into multiple ones based on a separator sign.


# Split syntax is dividing a string into multiple smaller strings based on a separator. In our case we are splitting based on blank space " ".

D.split(" ") --> ['Please', 'Fix', 'Thx.']

D.split(" ")[0] --> Please
D.split(" ")[1] --> Fix
D.split(" ")[2] --> Thx

In case we want print out all substrings from our original string without having to index we can loop like below. 
Learn more about looping in python here: 
https://www.pls-fix-thx.com/post/while-for-loop-python

#print out each out in my list.
for x in range(len(D.split(" "))):
    print (D.split(" ")[x])
    
#we can also retrieve our split item from reverse. 
D.split(" ")[-1] --> 'Thx.'

#we can count the number of items are split would generate with..
len(D.split(" ")) --> 3


Replace Syntax

'Replace function
'Replace a character with another one of your choosing.
D.replace(" ", "-") 'Please-Fix-Thx.'

Strip Syntax

#Strip syntax is similar to trim function in excel. 
#First let's assign D variable with some white spaces. 
D = '   ' + D + '   ' --> '   Please Fix Thx.   '

#leading and trailing spaces. 
D.strip() --> 'Please Fix Thx.'

#Remove whitespace before. 
D.lstrip() --> 'Please Fix Thx.   '

#Remove whitespace after
D.rstrip() --> '   Please Fix Thx.'

Removing whitespace in python

#note that strip function does not remove whitespace in the middle of the text. and there is no pre defined strip function, which achieves this. 

A practical way of solving this would be to convert the string to a list and the join the needed words with a single whitespace " ". 

" ".join(list(filter(len, D.split(" ")))) --> 'Please Fix Thx.'

another way to solve it in a similar way is the the use of lambda functions which is basically your own custom function.

" ".join(list(filter(lambda x: x != "", D.split(" "))))

Len Syntax

#We can also test that the previous statement really added more characters (chr) with the len syntax to get the length of the variable. 

#Length of string.
len(D) --> 21 chr

#Length of "Please Fix Thx."
len("Please Fix Thx.") --> 15chr.

Reverse Syntax

#This is perhaps not the most frequently used function ever. However I think it shows the effectiveness in python. that we in a few characters of code can reverse a string with the following syntax.

D[::-1] --> '.xhT xiF esaelP'

Find Syntax

#Return the starting position for a substring or a character. 
#note that it returns the first match. 

D.find("Fix") --> 7

#returns starting position for substring.1st "e" from the left.
D.find("e") --> 2

In case we have multiple of a given character and need to reverse search the textstring we can use the rfind syntax.

#1st "e" from the right
D.rfind("e") --> 5

Count Syntax

#Count number of times substring in string.
D.count("e") --> there are 2 e in "Please Fix Thx."

Converting to lowercase letters

D.lower() --> 'please fix thx.'

Converting to uppercase letter

D.upper() --> 'PLEASE FIX THX.'

Boolean functions for testing what kind of word it is.

#a few useful functions for testing what kind of word it is that will return a boolean response, which is either True or False.

#testing if the string is only numeric. 
D.isnumeric()

#testing if the string is only alphabetic. 
D.isalpha()

#testing if the string is alphabetic or numeric
D.isalnum()

#checks if text is only lower caps. 
D.islower()

#checks if text is only upper caps. 
D.isupper()

Learn more about Python here for all my posts: https://www.pls-fix-thx.com/python

If you have found this article or website helpful. Please show your support by visiting the shop below.





33 views0 comments
bottom of page