I'm trying to take a list of terms and output them in a specific format.
For example, I have a list of verbs:
verbs = ['eat', 'run', 'jump', 'play', 'walk', 'talk', 'send']
and I want my output to be a string, where each item in the list is also a string, delineated by a |
magic(verbs)
output:
"eat" | "run" | "jump" | etc.
I've tried using the .join method, but that just gives me one big string of the terms separated by pipes, instead of what I want. Any ideas? I'm pretty new to Python and I'm sorry if this is incredibly low-brow or something that has been answered elsewhere.
5 Answers 5
verbs = ['eat', 'run', 'jump', 'play', 'walk', 'talk', 'send']
x=(' | '.join('"' + item + '"' for item in verbs))
print(x)
you can see ouput here
Comments
Try with:
verbs = ['eat', 'run', 'jump', 'play', 'walk', 'talk', 'send']
print("|".join(['"'+e+'"' for e in verbs]))
Output:
"eat"|"run"|"jump"|"play"|"walk"|"talk"|"send"
Comments
This does what you want:
def magic(terms):
print(' | '.join('"%s"' % term for term in terms))
verbs = ['eat', 'run', 'jump', 'play', 'walk', 'talk', 'send']
magic(verbs) # -> "eat" | "run" | "jump" | "play" | "walk" | "talk" | "send"
Comments
verbs = ['eat', 'run', 'jump', 'play', 'walk', 'talk', 'send']
def magic(s):
return ' | '.join('"%s"' % x for x in s)
print(magic(verbs))
# "eat" | "run" | "jump" | "play" | "walk" | "talk" | "send"
Comments
Between each quote-enclosed verb, put a pipe:
" | ".join('"{0}"'.format(verb) for verb in verbs)
>>> verbs = ['eat', 'run', 'jump', 'play', 'walk', 'talk', 'send']
>>> print(" | ".join('"{0}"'.format(verb) for verb in verbs))
"eat" | "run" | "jump" | "play" | "walk" | "talk" | "send"
.join()to join them with a pipe.