Python program to separate numbers and letters from a string

Python program to separate numbers and letters from a string

In this article, we will going to solve an interesting question based on String Manipulation. We have to separate numbers and letters from a string.

Question - Write a Python program to separate numbers and letters from a string

So, without wasting any time let's solve this question. If you like the article content, then share it in your coding groups so that more people can take advantage of the content. 


Logic and Explanation

Question - Write a Python program to separate numbers and letters from a string

Python program to separate numbers and letters from a string - Suppose we are give a string "s". We also define two empty strings "n" and "a" to store number and alphabet which we will take out from the string "s".

s = "He1l3lo56"


To separate numbers and letter from a string we will first iterate through the list and then check using string in-built method to check whether the character is a number or alphabet. If it is a number then we will concatenate in string "n" and if it is an alphabet then we will concatenate it in string "a". I hope the logic is clear to you. Now, you  can check the code for Python program to separate numbers and letters from a string.

n = a = ""
for x in s:
    if x.isdigit():
        n += x
    if x.isalpha():
        a += x
print("Numbers in string=", n)
print("Alphabets in string=", a)

Output

Numbers in string= 1356
Alphabets in string= Hello

So, I hope you liked the article and you would have found the content of the article useful and helpful for you. Share this article in your coding communities so that more people can take advantage of the content. Also, In case of any doubt feel free to ask in the comments below.

Enjoy Coding!!!


Also Read :-





For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment

For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment (0)

Previous Post Next Post