w3resource logo
Python Exercises

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 of 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

Python Code Editor:

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

Improve this sample solution and post your code through Disqus