简体   繁体   中英

Convert input String to List after each character without spaces in python

I am trying to ask for an input of an alphanumeric phone number and convert it to a list so that I can convert the alphabet letters to numbers.

I have this for the input:

number=str(input("Enter Alphanumeric phone number:"))
number.split("")

and to try and convert it I tried

number.split("")

which gave me an error, and

list(number)

which just left it as a string.

Any ideas on what is going wrong?

One of the issues you have with both approaches is you still have to convert the string to a number in this case int .

numbers = str(input('Enter Alphanumeric phone number'))
numbers = list(numbers)
numbers = [int(number) for number in numbers]

for input of 1234567899 you'll get [1,2,3,4,5,6,7,8,9,9]

list(number) will convert the string to a list of characters. It sounds like you need to assign the result back to number though, like this:

number = list(number)

You can also use list comprehension for one line input. In this method you will not need to write more number of lines.

list = [int(i) for i in input()]

for input 123456789 you will get [1,2,3,4,5,6,7,8,9]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM