w3resource

Python: Separate and print the numbers of a given string

Python Regular Expression: Exercise-27 with Solution

Write a Python program to separate and print the numbers in a given string.

Sample Solution:

Python Code:

import re
# Sample string.
text = "Ten 10, Twenty 20, Thirty 30"
result = re.split("\D+", text)
# Print results.
for element in result:
    print(element)
	

Sample Output:

10                                                                                                            
20                                                                                                            
30

Pictorial Presentation:

Python: Regular Expression -  Separate and print the numbers of a given string.

Flowchart:

Flowchart: Regular Expression - Separate and print the numbers of a given string.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to match if two words from a list of words starting with letter 'P'.
Next: Write a Python program to find all words starting with 'a' or 'e' in a given string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.