Perform Various Operations On String Using String Libraries in Python

Implement Various Basic Operations On String

In this program, we will learn the different basic operations of string and perform various operations on a string using string libraries. 

In computer science, in the field of formal language theory, frequent use is made up of a variety of string functions; However, the notation used is different from that used for computer programming, and theoretically, some commonly used functions are rarely used when programming. This article defines some of these root words.

String Operators in Python:

  • upper() → It will convert lower case letter into upper case ( Capital Letter )
  • lower() → It will convert upper case letter into lower case ( Small Letter )
  • replace() → It is used to replace the letter.
  • strip() → It is used to remove the spaces.
  • lstrip() → It is used to remove the left side spaces string.
  • rstrip() → It is used to remove the right side spaces string.
  • startwith() → It is used to check to start the letter of the text.
  • endwith() → It is used to check to last the letter of the text.

Algorithm: Python String Operator

  •    Step1. Start
  •    Step2. Initialize upper case letters
    • capital=str1.upper()
  •    Step3. Initialize lower case letters
    • small=capital.lower()
  •    Step4. Replace str1 to str 2
    • str2=str1.replace(“students”,”engineers”)
  •    Step5. print left and right strips
    • left=st.lstrip()
    • right=st.rstrip()
  • Step6. print prefixes
    • prefix1=str1.startswith(“Welcome”)
  • Step7. End

Source Code: Python String Methods Examples

str1="Welcome you all students"

capital=str1.upper()

print(capital)

small=capital.lower()

print(small)

str2=str1.replace("students","engineers")

print(str2)

st=" This is an example of strip "

left=st.lstrip()

print(left)

right=st.rstrip()

print(right)

prefix1=str1.startswith("Welcome")

print(prefix1)

prefix2=str2.startswith("s")

print(prefix2)

prefix3=str2.endswith("w")

print(prefix3)

The output of the Python String Method

WELCOME YOU ALL STUDENTS
welcome you all students
Welcome you all engineers
This is an example of strip
This is an example of strip
True
False
False
>

Basic Operations On String

FAQ:

What are the basic string operations?

Basic string operations are upper(), lower(), replace(), startwith(), endwith(), lstrip(), rstrip(), etc…

Can you use == for strings in Python?

Yes, you can use ‘==’ for strings in python. Examples
print(‘pramod’==’pramod’) // True
print(‘pramod’==’yadav’) // False

How do you process a string in Python?

To process a string in python, we have to use the method like lower(), upper(), replace(), etc.
Example:
str = ‘Pramod Yadav’
print(upper(str)) // PRAMOD YADAV
print(lower(str)) // pramod yadav
print(replace(‘Yadav’, ‘Kumar’) // Pramod Kumar

Recommended Posts:

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