Python Program on String Methods
Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python
In this article, we will delve into two Python programs that involve string manipulations and checks. These programs demonstrate the flexibility and power of Python when it comes to handling string data.
The first program focuses on analyzing a given string to count the number of spaces, digits, and alphabets. The second program explores various string methods, including checks for uppercase, lowercase, alphanumeric content, numeric properties, and string replacement.
Prerequisites
- Basic understanding of string manipulation in Python
- Familiarity with loops and conditional statements
- Knowledge of string methods such as isspace(), isdigit(), isalpha(), isupper(), islower(), isalnum(), isdigit(),
- isnumeric(), isdecimal(), and replace()
- Basic knowledge of Python syntax
- Understanding of input/output operations in Python
Topic Explanation
Program 1 – focuses on analyzing user-input strings to gain insights into their composition. It prompts the user to input a string and then employs a for loop to iterate through each character in the string. Using conditional statements and string methods such as isspace(), isdigit(), and isalpha(), the program counts the number of spaces, digits, and alphabets in the input string. The final output displays the counts of spaces, digits, and alphabets, providing a practical example for beginners to understand Python’s string manipulation capabilities.
# Ask the user of input s = input("Enter a String: ") # Initialize variables to count spaces, alphabets, and digits sp = 0 al = 0 dg = 0 # Iterate through every character in input string for i in range(0, len(s)): #Check for space if s[i].isspace(): sp = sp + 1 # Check if the character is a digit elif s[i].isdigit(): dg = dg + 1 # Check if the character is an alphabet elif s[i].isalpha(): al = al + 1 # Display counts of spaces, digits, alphabets print("Total Space is %d" % sp) print("Total Digit is %d" % dg) print("Total alphabate is %d" % al)
Enter a String: DataFlair
Total Space is 0
Total Digit is 0
Total alphabate is 9
Code Explanation:
1. Prompt the user to input a string by calling input() and storing it in variable s
2. Initialize variables sp, al, dg to 0 to count spaces, alphabets and digits respectively
3. Iterate through each character in the input string s using a for loop from 0 to length of s
4. Inside for loop:
- Check if character is a space using .isspace() and if yes, increment sp counter
- Check if character is digit using .isdigit() and if yes, increment dg counter
- Check if character is alphabet using .isalpha() and if yes, increment al counter
5. After for loop, print the values of sp, dg and al variables to display counts of spaces, digits and alphabets in the input string.
Program 2 – on the other hand, involves a predefined string and showcases a variety of string methods. It checks whether the string is in uppercase or lowercase using isupper() and islower(), respectively. Additionally, it examines if the string contains only alphabetic characters (isalpha()), is alphanumeric (isalnum()), consists of digits (isdigit()), is a numeric value (isnumeric()), and is a decimal value (isdecimal()). The program concludes by demonstrating a string replacement operation, replacing occurrences of “java” with “python” in the predefined string. This program provides beginners with practical examples of using different string methods in Python.
s = "Data-Flair java Free Course java is good language " # Check if the string is in uppercase and print the result print(s.isupper()) # Check if the string is in lowercase and print the result print(s.islower()) # Check if the string contains only alphabets and print the result if s.isalpha(): print("Valid Name......") else: print("Invalid Name. Enter again......") # Check if the string is alphanumeric and print the result print(s.isalnum()) # Check if the string consists of digits and print the result print(s.isdigit()) # Check if the string is a numeric value and print the result print(s.isnumeric()) # Check if the string is a decimal and print the result print(s.isdecimal()) # Replace occurrences of "java" with "python" in the string s = s.replace("java", "python") # Print the modified string print(s)
False
False
Invalid Name. Enter again……
False
False
False
False
Data-Flair python Free Course python is good language
Code Explanation:
2. Check if s is in uppercase using .isupper() and print result
3. Check if s is in lowercase using .islower() and print result
4. Check if s contains only alphabets using .isalpha()
- If true, print “Valid Name”
- If false, print “Invalid Name. Enter again……”
5. Check if s is alphanumeric using .isalnum() and print result
6. Check if s contains only digits using .isdigit() and print result
7. Check if s is numeric value using .isnumeric() and print result
8. Check if s is decimal value using .isdecimal() and print result
9. Replace “java” with “python” in s using .replace()
10. Print the modified string s
Summary
In these Python programs, the first one analyzes user-input strings to count spaces, digits, and alphabets. The second program checks predefined strings for properties like uppercase, lowercase, alphanumeric content, and performs string replacement, showcasing Python’s string manipulation capabilities. These Python programs deepen our grasp of fundamental Python concepts, particularly in manipulating strings. The detailed explanations cover essential aspects of Python syntax and string operations, contributing to a broader understanding of the language.
Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google