Python : Mastering string part #2

Hi all,

Today we are moving one more step forward to master strings in Python. If you didn’t got chance to look at part 1 of mastering strings in Python have a look – https://knowledge-junction.in/2022/12/19/python-mastering-strings-part-1/

So let’s explore strings

In last article we discussed till string functions. So today we will start with “Find and replace functions”

Find and replace functions

# Here are some functions

# Program for functions related to find and replace functions
string1 = 'Education is not learning of many facts, but training of mind to think .'
print(string1)

# The len() function determines the length of a string
print('The length of a string is : ',len(string1))

# The 'in' returns true if the string contains the provided substring
print("Is 'Education' present in this string : ",'Education' in string1)

# The startswith() returns true if the string starts with provided string
print("Does the string starts with 'learning' : ",string1.startswith('learning'))

# The endwith() returns true if the string ends with provided strings
print("Does string ends with 'think. : '",string1.endswith('think.'))

# replace() replace all old substrings with new substrings
print("The new string is : ", string1.replace('.', '- ALBERT EINSTEIN'))

------------------------------------------------------------------
Output
Education is not learning of many facts, but training of mind to think .
The length of a string is :  72
Is 'Education' present in this string :  True
Does the string starts with 'learning' :  False
Does string ends with 'think. : ' False
The new string is :  Education is not learning of many facts, but training of mind to think - ALBERT EINSTEIN

Some more functions

# Program for functions related to find and replace functions
string2 = 'Education is not learning of many facts, but training of mind to think .'
print(string2)

# The count() counts the non - overlapping occurence of substring in a string
print("The number of times 'i' occurred in string : ", string2.count("i"))

# The find () returns the index of first occurrence of suplied substring in a string
print("The first occrence of 'to' in the string at index : ", string2.find("to"))
--------------------------------------------------------------------
Output
Education is not learning of many facts, but training of mind to think .
The number of times 'i' occurred in string :  7
The first occrence of 'to' in the string at index :  62

f strings

We have a separate article for f strings where you can understand the topic of f strings so have a look – https://knowledge-junction.in/2022/04/13/python-f-strings/

String Membership Test

'a' in 'program'
------------------

Output
True

'a' not in 'battle'
------------------
Output
False

Iterating through a string

We can iterate through a string using a for loop.

# Iterating through a string
count = 0
for letter in 'Hello World':
    if(letter == 'l'):
        count += 1
print(count,'letters found')
-----------------------------------
Output
3 letters found

We have very good series of Python articles, please have a look – https://knowledge-junction.in/category/python/

To make cool games in Python with pygame we also have a series, please have a look – https://knowledge-junction.in/dashboard/python-pygame-series/

To make awesome GUI in Python with tkinter – https://knowledge-junction.in/dashboard/python-gui-programming-with-tkinter-complete-tutorials/

Thank you for reading this article 🙂 and stay tuned for the next articles on strings

Have a nice day!!

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Microsoft 365

Subscribe now to keep reading and get access to the full archive.

Continue reading