Site icon DataFlair

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

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.

2. s.lower()

3. len(s)

4. s.count(“Data”)

5. s.find(“Indore”)

6. s.capitalize()

7. s.format()

8. s.title()

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.

Exit mobile version