+ 3
Convert input into list/tuple.
How to convert input into list and tuple? Conversion of input to list/tuple. Thanks in advance.
6 Antworten
+ 6
[edited]: All code in attached file!
You can achieve this with:
inp = 'aaa1hhh2bbb3'
#output = 123
res = [i for i in inp if i.isdigit()] # result is list ['1', '2', '3']
print(res)
res1 = ''.join(res) # result is '123' (string)
print(res1)
res2 = int(res1) # result is 123 (integer)
print(res2)
# or in a short way:
res1 = ''.join([i for i in inp if i.isdigit()]) # '123' (string)
res2 = int(''.join([i for i in inp if i.isdigit()])) # 123 (int)
https://code.sololearn.com/ch5tuyjHfQix/?ref=app
+ 5
input function give you a str (string).
If you want a list of words you can do this:
x=input()
y=x.split()
Split() function split you string by space
For example x= "aaa bbbb", so y = ['aaa', 'bbbb']
You can also split by another symbols like comma, slash etc.
+ 4
Similarly as already explained, the input will be split by space ( or by any other separator). First sample generates a tuple, second example generates a list.
lst1 = tuple(input('...: ').split(' '))
lst2 = list(input('...: ').split(' '))
+ 4
Thanks Lothar🙏🙏
+ 3
Kostia Gorbach
Lothar
Thanks for your answers..
+ 3
I want to split single letters or numbers on the basis of their opposites.
For eg. input = aaa1hhh2bbb3
So I want only the numbers from the input and remove all the letters.
output = 123
Please suggest this method also