Python Program on Methods of String Class

Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python

In this article, we will delve into various methods of the String class in Python, exploring how they can be utilized for effective string manipulation. These methods provide powerful tools for transforming and analyzing strings, showcasing the versatility of Python in handling textual data.

Prerequisites

  • Basic understanding of Python syntax, variables, and input/output operations.
  • Familiarity with fundamental concepts such as string formatting, counting, and searching.

Topic Explanation

The code snippet introduces multiple methods of the String class, each serving a distinct purpose. From altering the case of characters to searching for substrings, counting occurrences, and formatting strings, these methods demonstrate the extensive capabilities of Python in string manipulation. This article will elucidate each method and its application, providing practical insights for both beginners and intermediate Python developers.

Furthermore, the article explores the practical implications of these String class methods. It sheds light on how these techniques are essential for real-world applications, from data processing to text analysis. By delving into these practical aspects, the article aims to provide readers with a concrete understanding of how these methods can be applied in various scenarios. This practical insight is particularly valuable for developers looking to enhance their skills in Python string manipulation for everyday programming challenges.

Code:

# Methods of String class
s = "     Data-Flair    "
# Convert the string to uppercase and print it
print(s.upper())

# Convert the string to lowercase and print it
print(s.lower())

# Get the length of the string and print it
x = len(s)
print(x)

# Count the occurrences of the substring "Data" in the string and print the result
print(s.count("Data"))

# Find the index of the substring "Indore" in the string and print it
print(s.find("Indore"))

# Take user input for the name and capitalize the first letter, then print it
s = input("Enter your Name:")
print(s.capitalize())

# Take user input for roll number, name, and course
rno = input("Enter Roll No")
name = input("Enter Name")
course = input("Enter Course")

# Format and print a string using the provided roll number, name, and course
s = "My Roll no is {0} and Name is {1} and Course is {2}"
print(s.format(rno, name, course))

# Convert the given string to title case and print it
s = "data-Flair provide free python course Data-Flair also having Data related course Data is important"
print(s.title())

# Remove leading and trailing spaces from the string and print it
print(s.strip())

# Remove only the leading spaces from the string (Note: This line does not modify the original string)
s.lstrip()

# Remove only the trailing spaces from the string (Note: This line does not modify the original string)

Output:

DATA-FLAIR
data-flair
19
1
-1
Enter your Name: ram
Ram
Enter Roll No25
Enter Nameram
Enter Coursecomputer science
My Roll no is 25 and Name is ram and Course is computer science
Data-Flair Provide Free Python Course Data-Flair Also Having Data Related Course Data Is Important
data-Flair provide free Python course Data-Flair also having Data related course Data is important

Code Explanation:

1. s.upper()
This converts all the characters in the string to uppercase.

  • For example, “data-Flair” would become “DATA-FLAIR”
  • It returns a new string with the converted case, the original string s remains unchanged.

2. s.lower()

  • This converts all characters in the string to lowercase.
  • For example, “DATA-FLAIR” would become “data-flair”

3. len(s)

  • This returns the length of the string, i.e. the number of characters.
  • This is assigned to a variable x and printed.

4. s.count(“Data”)

  • This counts how many times the substring “Data” occurs within s.
  • It performs a case-sensitive search.
  • The number of occurrences is printed.

5. s.find(“Indore”)

  • This searches for the substring “Indore” within s and returns the index of first match.
  • If no match is found, it returns -1.
  • The return value indicates the index position in s where “Indore” begins.

6. s.capitalize()

  • This capitalizes the first letter of the string while making other letters lowercase.
  • Takes user input string s, capitalizes first letter and prints the result string.

7. s.format()

  • This formats the string using {} placeholders and specified parameters.
  • User inputs (rno, name, course) are formatted into the result string
  • Prints formatted string with placeholders replaced by parameters.

8. s.title()

  • Converts string s to title case – first letter of each word is capitalized
  • For example “data-flair” becomes “Data-Flair”.
  • Prints the title case converted string.

Summary

This article explores various methods of the String class in Python, showcasing their applications for effective string manipulation. The discussed methods cover a range of functionalities, including changing character case, searching for substrings, counting occurrences, and formatting strings. The article provides practical insights for both beginners and intermediate Python developers, assuming a basic understanding of Python syntax and string concepts.

The article also includes prerequisites, suggesting a foundational understanding of Python syntax, variables, and basic string concepts for optimal comprehension. Overall, the provided examples and explanations serve as a valuable resource for those looking to enhance their skills in Python string manipulation.

Did we exceed your expectations?
If Yes, share your valuable feedback on Google

courses
Image

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *