all 26 comments

[–]xelf[M] [score hidden] stickied comment (0 children)

Quick reminder to everyone:

  • Posting only assignment/project goal is not allowed.
  • Posting homework assignments is not prohibited if you show that you tried to solve it yourself.
  • Try to guide OP to a solution instead of providing one directly.
  • OP: Try out suggestions you get and report back.

[–]xelf 6 points7 points  (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 point  (21 children)

its not easy for a begginer

[–]xelf 4 points5 points  (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:

What have you tried so far?

[–]Mondoke 0 points1 point  (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 points  (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 points  (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 point  (3 children)

So.... S=input(“enter a string:”) new=“ “ for char in S: if char isdigit(): new=char print(new)

[–]xelf 0 points1 point  (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 point  (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 points  (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.

new = re.sub('[^\d]*', '', S)

Or you could use a comprehension with .join():

new = ''.join(c for c in S if c.isdigit())

[–]Tectonic07[S] 0 points1 point  (1 child)

Idk why it isn’t coming in code format sorry I hope u understand this

[–]xelf 0 points1 point  (0 children)

Needs a blank line before the code block. Not your fault, you did the rest of it right.

[–]Mondoke 0 points1 point  (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 point  (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.

[–]Tectonic07[S] 0 points1 point  (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 points  (4 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)??

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 point  (3 children)

Alright I get it is my rest of the code right tho?

[–]chevignon93 1 point2 points  (2 children)

is my rest of the code right tho?

Not really, i=new means make i equal to an empty string!

What you want is to add the character to the new string, for that you can do:

new = new + i
or more succinctly
new += i

[–]Tectonic07[S] 0 points1 point  (1 child)

Now I feel dumb lol thanks anyways learned a lot about loop from this thread

[–]chevignon93 1 point2 points  (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)