In the previous article, we have discussed Python Program to Add Trailing Zeros to String
Given a string and the task is to extract only characters (alphabets) from a given string.
Examples:
Example1:
Input:
Given String = hello 123 this is Btech Geeks python3 coding platform54231
Output:
The Given String after extracting only characters = hellothisisBtechGeekspythoncodingplatform
Example2:
Input:
Given String=good morning23this is btechgeeks@19python
Output:
The Given String after extracting only characters = goodmorningthisisbtechgeekspython
Program to Extract Only Characters from a Given String
Below are the ways to Extract Only Characters from a given String. Here we used ASCII values to get the result.
Method #1: Using ASCII Values (Static Input)
Approach:
- Give the string as static input and store it in the variable.
- Take a new empty string to store only characters from the above-Given String.
- Traverse in this given string using the For loop.
- Inside the for loop, Check each letter of the above-given string is only a character or not by using the ord( ) method.
- Ord( ) method gives the ASCII values of the character.
- Check the elements of the given string are Upper case Characters by using if conditional statement and concatenate it to the above declared new string.
- Else Check for the Lower case Characters by using Elif conditional statement and concatenate it to the above declared new string.
- Print the above declared new String(has only characters).
- The Exit of the Program.
Below is the implementation:
# Give the string as static input and store it in the variable. gven_stng = 'hello 123 this is Btech Geeks python3 coding platform54231' # Take a new empty string to store only characters from the above Given String only_chrs = "" # Traverse in this given string using the For loop for chrs in gven_stng: # Inside the for loop, Check each letter of the above given string is only a character # or not by using ord( char) method . # Ord( ) method gives the ASCII values of the character. # Check the elements of the given string are Upper case Characters by using if conditional # statement and concat it to the above declared new string. if ord(chrs) >= 65 and ord(chrs) <= 90: only_chrs += chrs # Else Check for the Lower case Characters by using Elif conditional statement and # concat it to the above declared new string. elif ord(chrs) >= 97 and ord(chrs) <= 122: only_chrs += chrs # print the above declared new String(has only characters). print('The Given String after extracting only characters =',only_chrs)
Output:
The Given String after extracting only characters = hellothisisBtechGeekspythoncodingplatform
Method #2: Using ASCII Values (User Input)
Approach:
- Give the string as user input using the input() function and store it in the variable.
- Take a new empty string to store only characters from the above-Given String.
- Traverse in this given string using the For loop.
- Inside the for loop, Check each letter of the above-given string is only a character or not by using the ord( ) method.
- Ord( ) method gives the ASCII values of the character.
- Check the elements of the given string are Upper case Characters by using if conditional statement and concatenate it to the above declared new string.
- Else Check for the Lower case Characters by using Elif conditional statement and concatenate it to the above declared new string.
- Print the above declared new String(has only characters).
- The Exit of the Program.
Below is the implementation:
# Give the string as user input using the input() function and store it in the variable. gven_stng = input('Enter some random string = ') # Take a new empty string to store only characters from the above Given String only_chrs = "" # Traverse in this given string using the For loop for chrs in gven_stng: # Inside the for loop, Check each letter of the above given string is only a character # or not by using ord( char) method . # Ord( ) method gives the ASCII values of the character. # Check the elements of the given string are Upper case Characters by using if conditional # statement and concat it to the above declared new string. if ord(chrs) >= 65 and ord(chrs) <= 90: only_chrs += chrs # Else Check for the Lower case Characters by using Elif conditional statement and # concat it to the above declared new string. elif ord(chrs) >= 97 and ord(chrs) <= 122: only_chrs += chrs # print the above declared new String(has only characters). print('The Given String after extracting only characters =', only_chrs)
Output:
Enter some random string = good morning23this is btechgeeks@19python The Given String after extracting only characters = goodmorningthisisbtechgeekspython
Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.