Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print only number
#1
Hi I need to print only numbers from my code. So when someone types for example "Hello123" it prints only 123. How can I do that? Thanks
name = input("Jak se jmenuješ? ")
str = name
print ("Ahoj " + str + "!")
Reply
#2
You should check out our tutorial for format method

since you didnt specify any restrictions....regex would probably be the least amount of code
>>> import re
>>> re.sub('[^0-9]', '', 'Hello123')
'123'
>>> re.sub('[^0-9]', '', '!@#$%^&Hello123sdggjfhd')
'123'
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I print the number of unique elements in a list? AnOddGirl 5 3,244 Mar-24-2020, 05:47 AM
Last Post: AnOddGirl
  Print the Largest Odd Number Than999 5 4,161 Jul-24-2019, 11:35 AM
Last Post: jefsummers
  How to print a statement if a user's calculated number is between two floats Bruizeh 2 2,370 Feb-10-2019, 12:21 PM
Last Post: DeaD_EyE
  Program to print: Last Name, ID, Mobile Number, All panick1992 14 9,744 Mar-15-2017, 02:46 PM
Last Post: panick1992

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020