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: Startstep 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: endSource 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
Recommended Post:
- Python Hello World Program
- Python Comment | Creating a Comment | multiline comment | example
- Python Dictionary Introduction | Why to use dictionary | with example
- How to do Sum or Addition in python
- Python Reverse number
- find the common number in python
- addition of number using for loop and providing user input data in python
- Python Count char in String
- Python Last Character from String
- Python Odd and Even | if the condition
- Python Greater or equal number
- Python PALINDROME NUMBER
- Python FIBONACCI SERIES
- Python Dictionary | Items method | Definition and usage
- Python Dictionary | Add data, POP, pop item, update method
- Python Dictionary | update() method
- Delete statement, Looping in the list In Python
- Odd and Even using Append in python
- Python | Simple Odd and Even number
Get Salesforce Answers