EHM
EHM

Reputation: 959

How to print strings in line in python 3.x

What I wanted to do is print strings in line inside a for loop, I know it is possible in python 2.x, but it doesn't seem to work for 3.x versions, so the question is, is there a way to do this in python 3 versions. To make it specific

user_input=3
for counter in range(user_input):
    print('| ')

what this code would give me is

|
|
|

but what i actualy want is to do something like | | |

Upvotes: 1

Views: 1089

Answers (4)

whackamadoodle3000
whackamadoodle3000

Reputation: 6748

You could also do this string multiplication:

user_input=3
print(user_input*'| ')

to get that output, which is simpler than changing the ending of print.

Upvotes: 1

Nick Porter
Nick Porter

Reputation: 173

The print function has an end parameter in python3 that defaults to '\n' . If you use print('| ', end='') you will get the result you desire.

Upvotes: 2

chevybow
chevybow

Reputation: 11948

I think this is what you're looking for

user_input=3
for counter in range(user_input):
    print('|', end=' ')

Basically just have to add that you want to end the string with a space instead of defaulting to a new line when dynamically printing

Upvotes: 2

Rezney
Rezney

Reputation: 371

Please try:

user_input=3
for counter in range(user_input):
    print('| ', end='')

https://docs.python.org/3.3/library/functions.html#print

Upvotes: 2

Related Questions