Last updated: Apr 9, 2024
Reading timeยท5 min
To print on the same line:
for
loop to iterate over the sequence.print()
function with each value and set the end
argument to a
space.print(item, end=' ')
.my_list = ['bobby', 'hadz', 'com'] # โ print list items horizontally in for loop for item in my_list: print(item, end=' ') # ๐๏ธ bobby hadz com # ------------------------------------------- # โ print horizontally using iterable unpacking print(*my_list) # ๐๏ธ bobby hadz com # ------------------------------------------- # โ print horizontally using str.join() result = ' '.join(my_list) print(result) # ๐๏ธ bobby hadz com
The first example uses a for loop and the
end
argument of the print() function to
print on the same line.
my_list = ['bobby', 'hadz', 'com'] for item in my_list: print(item, end=' ') # ๐๏ธ bobby hadz com
The end
argument is printed at the end of the message.
By default, end
is set to a newline character (\n
).
print('a', 'b', 'c') # ๐๏ธ 'a b c\n' print('a', 'b', 'c', end='') # ๐๏ธ 'a b c'
We set the argument to a space to print the list items horizontally, separated by spaces.
Alternatively, you can use the iterable unpacking operator.
The iterable unpacking operator will unpack the items of the iterable in the
call to the print()
function.
my_list = ['bobby', 'hadz', 'com'] print(*my_list) # ๐๏ธ bobby hadz com
The * iterable unpacking operator enables us to unpack an iterable in function calls, in comprehensions and in generator expressions.
print()
function.By default, the items of the iterable get printed horizontally, separated by spaces.
You can pass the
sep
keyword argument to the print()
function to change the separator.
# ๐๏ธ without separator print(*my_list, sep='') # ๐๏ธ bobbyhadzcom # ๐๏ธ with hyphen separator print(*my_list, sep='-') # ๐๏ธ bobby-hadz-com
The sep
argument is the separator between the arguments we pass to print()
.
By default, the argument is set to a space.
Alternatively, you can use the str.join()
method.
This is a two-step process:
str.join()
method to join the items of the iterable into a string.print()
function to print the string.# โ Print a list of strings horizontally my_list = ['bobby', 'hadz', 'com'] result = ' '.join(my_list) print(result) # ๐๏ธ bobby hadz com # --------------------------------------- # โ Print a list of numbers horizontally my_list = [2, 4, 8] result = ' '.join(str(number) for number in my_list) print(result) # ๐๏ธ 2 4 8
We used the str.join()
method to join the items in the list into a string.
The str.join() method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.
TypeError
if there are any non-string values in the iterable.If your iterable contains numbers or other types, convert all of the values to
strings before calling join()
.
my_list = [2, 4, 8] result = ' '.join(str(number) for number in my_list) print(result) # ๐๏ธ 2 4 8
The string the method is called on is used as the separator between the elements.
We used a space in the examples, but you can use any other separator.
my_list = ['bobby', 'hadz', 'com'] result = '-'.join(my_list) print(result) # ๐๏ธ bobby-hadz-com
Pass a string to the input()
function to have a print statement and input on
the same line.
# โ Print input message on the same line username = input('Enter your username: ') print(username) # ---------------------------------- # โ Use print() and input() without separator and newline character print( 'Your username is: ', input('Enter your username: '), sep='', end='' )
In the first example, we passed a prompt string to the input()
function.
The input function takes an optional prompt
argument and writes it to standard output without a trailing newline.
s = input('Enter your name: ') print(s)
The function then reads the line from the input, converts it to a string and returns the result.
You can also use the print()
function before or after the input()
function.
print('this runs before') user_input = input('Enter your username: ') print('this runs after')
If you use the print()
function instead of the prompt
argument, the print
statement is printed on a separate line.
print('Enter your name: ') s = input() print(s)
If you want to call the print()
function with multiple arguments without a
separator, set the sep
and end
keyword arguments to empty strings.
print( 'Your username is: ', input('Enter your username: '), sep='', end='' )
The sep
argument is the separator between the arguments we pass to print()
.
By default, the argument is set to a space.
print('a', 'b', 'c') # ๐๏ธ 'a b c' print('a', 'b', 'c', sep='') # ๐๏ธ 'abc'
The end
argument is printed at the end of the message.
By default, end
is set to a newline character (\n
).
print('a', 'b', 'c') # ๐๏ธ 'a b c\n' print('a', 'b', 'c', end='') # ๐๏ธ 'a b c'
You can also use a formatted string literal to print multiple arguments on the same line.
print(f'Your name is: {input("Enter your name: ")}')
Formatted string literals (f-strings) let us include expressions inside of a
string by prefixing the string with f
.
my_str = 'is subscribed:' my_bool = True result = f'{my_str} {my_bool}' print(result) # ๐๏ธ is subscribed: True
Make sure to wrap expressions in curly braces - {expression}
.
You can learn more about the related topics by checking out the following tutorials: