-1

I have one problem with print in Python, I am starting to learn Python, but when I want to print variables in print function, it looks like that after one variable it adds newline to the outcome:

print(game_name + " | " + rating)

I am making a game database with my own ratings on the games but if it prints the game and the rating there is like one empty line belpw the game_name and rating is written, how can I delete that empty newline? I am very new to this, so please don't be mean...

Luky
  • 11
  • Hi! Does this help? https://stackoverflow.com/questions/493386/how-to-print-without-newline-or-space It is not very clear whether your empty line is between game_name and rating, or after game name and rating. – smagnan May 14 '21 at 18:39

4 Answers4

0

Welcome to Stack Overflow! The most likely culprit is that there is a newline at the end of the game_name variable. The easy fix for this is to strip it off like this:

print(game_name.strip() + " | " + rating)
Cargo23
  • 3,064
  • 16
  • 25
0

Say we had two variables like this.

game_name = 'hello\n'
rating = 'there'

game_name has the newline. To get rid of that use strip().

print(game_name.strip() + " | " + rating)

output

hello | there
Buddy Bob
  • 5,829
  • 1
  • 13
  • 44
0

If you want to remove the line break after printing, you can define the optional end parameter to the print statement.

print('Hello World', end='') # No new line

If your variable has a new line added to it, and you want to remove it, use the .strip() method on the variable.

print('Hello World\n'.strip()) # No empty line

For your code, you could run it:

print(game_name + " | " + rating, end='')

Or

print(game_name + " | " + rating.strip())

If the error is that a new line is printed after game_name, you'll want to call the strip method on that instead.

print(game_name.strip() + " | " + rating)
PYer
  • 458
  • 3
  • 12
  • Okay, thank you guys so much for your help. <3 – Luky May 14 '21 at 18:45
  • @Luky If an answer fixes your problem, you should probably mark it as the answer using the grey checkmark. People who have the same question can find this post, and see the answer. – PYer May 14 '21 at 18:48
0

rating or game_name most likely have a new line after the specified string.

You can fix it by doing this:

game_name = game_name.strip('\n')
rating = rating.strip('\n')
print(game_name + " | " + rating)
Quessts
  • 440
  • 2
  • 19