简体   繁体   中英

How do you remove quotation marks form a list in python?

if deliver == 1 :
    address = list(input("What is your address?"))


print("Your pizza will be delivered to", address)

When it prints it does 'p' 'p' 'o' 'p' '1' '1' when i want it to do pp op11 so it can take in real address

So 2 ideas here, I will show you how to fix your current problem and then I will help you see a bigger problem.

Here is how to fix your current problem.

if deliver == 1 :
    address = list(input("What is your address?"))


print("Your pizza will be delivered to", ''.join(address))

Notice the ''.join(address).

.join: tell python to concatenate each item in a list.

.join(list): list is the list that you need to concatenate.

''.join(list): '' tells .join to add nothing between each item in the list

  • '-'.join(['888', '123', '4567']) would create '888-123-4567'.

How to fix the bigger problem.

You are using input() which will return a string.

You then wrapped that string in a list. When a string is turned into a list each character, including spaces are turned into list items.

Once you understand that it may help you understand what is going wrong.

The problem here is that the list method is taking the input string, let's say 'Address1' and converting it to ['A', 'd', 'd', 'r', 'e', 's', 's', '1'] that's why you are seeing the quotation marks.

you can do something like this:

print("Your pizza will be delivered to", input("What is your address?"))

and avoid extra processing.

Be aware that if deliver != 1 this code will raise a NameError exception, because (I don't know the rest of your code) but the address variable could not be defined.

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