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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python-exercises/re/python-re-exercise-27.php