Python Various Operation on String| Python String Method

Q) Implement python various operations on a string using string libraries with an algorithm.

In python string method program, we will learn different string operations. We will try to implement all string operations. There are different string operation and some are given below:
  • upper()
  • lower()
  • replace()
  • strip()
  • startwith()
  • endwith()

Aim:

To perform  various operation on a string using string libraries with an algorithm

Algorithm to implement String Operation

step 1: Start step 2: initialise upper case letters           capotal = str.upper() step 3: Initialise upper case letter             small = capital.lower() step 4: Replace str1 to str2          str2 = str1.replace(“students’,”engineer”) step 5: print left and right strips           left  = str1.strip()           right = str1.strip() step 6: print prefixes           prefix1 = str1.startwith(“wel come”) step 7: end

Source code:

str1 = "welcome to all student"
capital = str1.upper()
print(capital)
str2 = "MADAMAPALLE INSTITUTE OF TECHLONOLOGY AND SCIENCE is an engineering college"
lower = str2.lower()
print(lower)
str3 = str2.replace("MADAMAPALLE INSTITUTE OF TECHLONOLOGY AND SCIENCE","MITS")
print(str3)
str4 = "     This is an example of strip        "
left = str4.strip()
print(left)
str1 = "welcome to all student"
prefix1 = str1.startswith('hellow')
print(prefix1)
prefix2 = str1.startswith('wel')
print(prefix2)
prefix3 = str1.endswith('student')
print(prefix3)

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
WELCOME TO ALL STUDENT
madamapalle institute of techlonology and science is an engineering college
MITS is an engineering college
This is an example of strip
False
True
True

Process finished with exit code 0
To Know about each and every string operation briefly scroll down.

What are the different string operations? with source code and explanation

Here we will discuss all string operations one by one.

UPPER():

upper() string operation is used to convert all the lower case letters into upper case.

Syntax:

string_variable_name.upper()

Example:

str1 = "welcome to all student"
capital = str1.upper()
print(capital)
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
WELCOME TO ALL STUDENT

Process finished with exit code 0

LOWER():

lower() string operation is used to convert all the upper case letters into lower case.

Syntax:

string_variable_name.lower()

Example:

str2 = "MADAMAPALLE INSTITUTE OF TECHLONOLOGY AND SCIENCE"
lower = str2.lower()
print(lower)
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
madamapalle institute of techlonology and science

Process finished with exit code 0

REPLACE():

replace() string operation is used to replace the word of sentences.

Syntax:

string_variable_name.replace('word1','word2')

Example:

str2 = "MADAMAPALLE INSTITUTE OF TECHLONOLOGY AND SCIENCE is an engineering college"
str3 = str2.replace("MADAMAPALLE INSTITUTE OF TECHLONOLOGY AND SCIENCE","MITS")
print(str3)
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
MITS is an engineering college

Process finished with exit code 0

STRIP():

It is used to remove or delete the space before the sentence and after the sentence. strip() operation is used basically in email and password or any form to replace the spaces before the name. If we do not use strip() then when we find the name of the candidate then we can not find it because they used space while entering the name but we are searching without space.

Syntax:

string_variable_name.strip()

Example:

Assume there is a sentence given ”      He is a Ram.     ”  In this sentence you can that there are spaces before to start the sentence and after the sentence. so to remove that we are using strip() operation.
str4 = "     This is an example of strip        "
left = str4.strip()
print(left)
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
This is an example of strip

Process finished with exit code 0

STARTSWITH():

startswith() is used to find the sentence is starting with the word which you are finding or not. If it is starting with the same word then it will Print “True” otherwise it will print “False”.

Syntax:

string_variable_name.startswith()

Example:

str1 = "welcome to all student"
prefix1 = str1.startswith('hellow')
print(prefix1)
prefix2 = str1.startswith('wel')
print(prefix2)
prefix3 = str1.endswith('student')
print(prefix3)
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
False
True
True

Process finished with exit code 0
implement python various operations on a string

Recommended Post:

Get Salesforce Answers

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment