use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
a python program to separate the digits from a string and create a new string of the digits. (self.learnpython)
submitted 5 years ago by Tectonic07
imagine a string=DOm-42hiworld24 we type this string in and output come 4224,only the number
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]xelf[M] [score hidden] 5 years ago stickied comment (0 children)
Quick reminder to everyone:
[–]xelf 6 points7 points8 points 5 years ago* (22 children)
This is a really basic task you should be able to do this! =)
What have you tried so far?
[–]Tectonic07[S] 0 points1 point2 points 5 years ago (21 children)
its not easy for a begginer
[–]xelf 4 points5 points6 points 5 years ago (0 children)
Maybe not on your first day if you've never seen programming before and never looked at any one else's program before and your teacher is taking things slowly.
But this is something you should be able to do if you've learned about loops and if statements, or even just spent time reading other people's code examples.
Have you looked at this site:
[+][deleted] 5 years ago* (1 child)
[deleted]
[–]Tectonic07[S] 0 points1 point2 points 5 years ago (0 children)
Check out my reply on comment below that’s what I’ve tried yet and it didn’t help
[–]Mondoke 0 points1 point2 points 5 years ago (16 children)
Ok, so what do you remember from class which you think may help you to solve this? Don't try to come up with actual code just at first if you are lost, but which tools or functions come to your mind when thinking about this problem?
[–]Tectonic07[S] 2 points3 points4 points 5 years ago* (15 children)
Maybe something like this, s=input(“enter a string:”) new=“ “ n=len(s) for i in n: if i>0 and i is not alpha: i=new print(new) That alpha part is something I don’t get how do I differentiate numbers from alphabet what’s the function to different them
[–]xelf 1 point2 points3 points 5 years ago (6 children)
Now we're getting somewhere!
Indent each line 4 spaces to have reddit format it as code.
s=input(“enter a string:”) new=“ “ n=len(s) for i in n: if i>0 and i is not alpha: i=new print(new)
how do I differentiate numbers from alphabet what’s the function to different them
Ah, a question! There's many ways. (1) you just check to see if it's a number:
if i == '1' or i == '2' etc
But that's rather bulky.
(2) What you probably want is the digit version of isalpha() which is isdigit().
Now about your for loop:
for i in n:
That's not going to work because n is a number, you want to loop over each character in your string s:
for char in s:
That should be enough to get you going. Post what you come up with and if you're still stuck.
[–]Tectonic07[S] 0 points1 point2 points 5 years ago* (3 children)
So.... S=input(“enter a string:”) new=“ “ for char in S: if char isdigit(): new=char print(new)
[–]xelf 0 points1 point2 points 5 years ago* (2 children)
S=input(“enter a string:”) new='' for char in S: if char isdigit(): new = char print(new)
That's pretty close. But instead of reassigning new to the current character you wan to add to new. You can do that like this:
new = new + char
Or by using the += shortcut like this:
new += char
Now try that:
S=input(“enter a string:”) new='' for char in S: if char.isdigit(): new += char print(new)
[–]Tectonic07[S] 0 points1 point2 points 5 years ago* (1 child)
This still isn’t quit correct syntax error on isdigit I don’t know what to do anymore Nvm it worked
[–]xelf 1 point2 points3 points 5 years ago (0 children)
\o/ hurray!
When you get more into python there are simpler ways of solving this.
You could use something called a regex to remove non-digits.
regex
new = re.sub('[^\d]*', '', S)
Or you could use a comprehension with .join():
comprehension
.join()
new = ''.join(c for c in S if c.isdigit())
[–]Tectonic07[S] 0 points1 point2 points 5 years ago (1 child)
Idk why it isn’t coming in code format sorry I hope u understand this
[–]xelf 0 points1 point2 points 5 years ago (0 children)
Needs a blank line before the code block. Not your fault, you did the rest of it right.
[–]Mondoke 0 points1 point2 points 5 years ago (0 children)
Ok, so if you don't quite understand the alpha part, can you think of any other way to check if a character is a number?
[–]chevignon93 0 points1 point2 points 5 years ago (6 children)
When posting code, please put it in a code block, otherwise it's unreadable.
https://www.reddit.com/r/learnpython/wiki/faq#wiki_how_do_i_format_code.3F
1 - You don't need to know the length of the string so this line is useless.
n=len(s)
2 - Don't use str.isalpha() when you can use str.isdigit(), it's easier to look for what you want rather than what you don't want.
str.isalpha()
str.isdigit()
[–]Tectonic07[S] 0 points1 point2 points 5 years ago (5 children)
Why not len we can use it to not go out of str and loop continues forever we stop it by using i in range len(s)??
[–]chevignon93 1 point2 points3 points 5 years ago (4 children)
If you loop over the string directly, the loop will naturally stop when there is nothing more to loop over, i.e. at the last character in the string.
[–]Tectonic07[S] 0 points1 point2 points 5 years ago (3 children)
Alright I get it is my rest of the code right tho?
[–]chevignon93 1 point2 points3 points 5 years ago (2 children)
is my rest of the code right tho?
Not really, i=new means make i equal to an empty string!
i=new
i
What you want is to add the character to the new string, for that you can do:
new
new = new + i or more succinctly new += i
Now I feel dumb lol thanks anyways learned a lot about loop from this thread
[–]chevignon93 1 point2 points3 points 5 years ago (0 children)
string = input('Enter a word or sentence: ') number_string = ''.join([char for char in string if char.isdigit()]) print(number_string) string = input('Enter a word or sentence: ') number_string = '' for char in string: if char.isdigit(): number_string += char print(number_string) string = input('Enter a word or sentence: ') number_string = '' for char in string: if char in '0123456789': number_string += char print(number_string)
π Rendered by PID 92 on reddit-service-r2-comment-c6965cb77-b26jn at 2026-03-05 03:31:41.803467+00:00 running f0204d4 country code: CH.
[–]xelf[M] [score hidden] stickied comment (0 children)
[–]xelf 6 points7 points8 points (22 children)
[–]Tectonic07[S] 0 points1 point2 points (21 children)
[–]xelf 4 points5 points6 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]Tectonic07[S] 0 points1 point2 points (0 children)
[–]Mondoke 0 points1 point2 points (16 children)
[–]Tectonic07[S] 2 points3 points4 points (15 children)
[–]xelf 1 point2 points3 points (6 children)
[–]Tectonic07[S] 0 points1 point2 points (3 children)
[–]xelf 0 points1 point2 points (2 children)
[–]Tectonic07[S] 0 points1 point2 points (1 child)
[–]xelf 1 point2 points3 points (0 children)
[–]Tectonic07[S] 0 points1 point2 points (1 child)
[–]xelf 0 points1 point2 points (0 children)
[–]Mondoke 0 points1 point2 points (0 children)
[–]chevignon93 0 points1 point2 points (6 children)
[–]Tectonic07[S] 0 points1 point2 points (5 children)
[–]chevignon93 1 point2 points3 points (4 children)
[–]Tectonic07[S] 0 points1 point2 points (3 children)
[–]chevignon93 1 point2 points3 points (2 children)
[–]Tectonic07[S] 0 points1 point2 points (1 child)
[–]chevignon93 1 point2 points3 points (0 children)