Reputation: 2009
If I have a variable that prints out vertically like this:
h
e
l
l
o
How do I make this variable print horizontally like this?:
hello
thank you for your help.
Upvotes: 1
Views: 27382
Reputation: 1
Just put *
in front of the text.
For example:
get_text=input()
print(*get_text)
Input: hello
Output: h e l l o
Upvotes: 0
Reputation: 46805
Join the representation of the list's items with ', '
,
l = [3.14, 'string', ('tuple', 'of', 'items')]
print(', '.join(map(repr, l)))
Output:
3.14, 'string', ('tuple', 'of', 'items')
I don't know what is meant by a "vertical list" as python only has lists without orientation.
Upvotes: 0
Reputation: 368
a very shortcut method I can Suggest is like follows
str1 = "I love python"
for chars in str1:
print( * [chars], end = ' ')
output
I l o v e p y t h o n
if you dont want space just remove the space between the single quote
i.e
print( * [chars], end = '')
output
I love python
Upvotes: 0
Reputation: 133574
Assuming
text = 'h\ne\nl\nl\no'
here is another way.
''.join(text.split())
Upvotes: 5
Reputation: 20510
It sounds like you are creating a string from an array? Or maybe a string from a string?
>>> from_list = ['h', 'e', 'l', 'l', 'o']
>>> print ''.join(from_list)
hello
>>> print ':'.join(from_list)
h:e:l:l:o
The join() function of strings takes a list and returns a string with its argument stuck between each item.
You might be using a string:
>>> from_string = "h\ne\nl\nl\no"
>>> print from_string
h
e
l
l
o
>>> print from_string.split()
['h', 'e', 'l', 'l', 'o']
>>> print "".join(from_string.split())
hello
>>> print " (pause) ".join("one, two, three, five, no four!".split(","))
one (pause) two (pause) three (pause) five (pause) no four!
This uses the split() function of strings, which splits a string into a list of items. The result is then joined back into a string. You can split on any string, but whitespace is the default.
Upvotes: 1