Home > Net >  How to get elements out of a list in Python?
How to get elements out of a list in Python?

Time:08-13

for example supposedly I have a list like this name=['John','Geoffrey','Travis'] how do I transform this list to something like

John Geoffrey Travis ? Thank you

CodePudding user response:

If you would like to get elements out of a list in Python you can just name a variable that has those elements so that they aren't listed.

name = ['John', 'Geoffrey', 'Travis'] # Giving a list some elements
print(name)
# Now changing this so that its all together and not bound to a list that prints every element with brackets
names = "John Geoffrey Travis" # Putting all elements together in a variable 
print(names)

Please correct me if I didn't answer your question correctly, it was pretty difficult to understand what you meant by how you explained what you want to do with your code.

CodePudding user response:

You can turn array elements into strings by putting square brackets after the array variable name with the number of the desired element inside of them. For example: name[0] would result in the string John. Remember arrays begin at zero.

I would recommend looking up Python programming tutorials and asking Google before you resort to asking questions on Stack Overflow.

CodePudding user response:

See str.join for more information.

name = ['John','Geoffrey','Travis']
s = str.join(' ', name)
print(s)
  • Related