Home > Enterprise >  Python remove the trailing white space when inputting a string of numbers
Python remove the trailing white space when inputting a string of numbers

Time:10-25

I'm looking for a solution to removing the trailing white space when inputting a string of numbers.

Code

vals_inp=input()
print('vals_inp',vals_inp)

vals_strip = vals_inp.rstrip() #edited
print('vals_strip', vals_strip, type(vals))

in_set=set(vals_strip) # problem happens here

OUTPUT

in_set {'1', '2', '3', ' '} #PROBLEM ' '

Things tried

I have tried rstrip lstrip rstrip and strip(' ') I see lots of answers using rstrip etc but its not working for me

Why it matters

The ' ' in my set is messing up the subsequent code. All help appreciated

CodePudding user response:

vals_inp=input() print('vals_inp',vals_inp)

vals_strip = vals_inp.rstrip() print('vals_strip', vals_strip, type(vals))

in_set=set(vals_strip) # problem happens here

CodePudding user response:

Thanks to @Awais Khan

vals_inp=input() 
list_set = list(vals_inp) 
vals = [x for x in list_set if x != ' '] 
set_vals = set(vals) 

OUTPUT: {'1', '3', '2'}

  • Related