Explain Codes LogoExplain Codes Logo

How can I print variable and string on same line in Python?

python
functions
formatting
strings
Alex KataevbyAlex Kataev·Feb 10, 2025
TLDR

Print a variable with a string on the same line using the print() function combined with + for concatenation, or preferably, an f-string for clean, manageable and easy-to-read code:

variable = "world" print("Hello " + variable) # Concatenation, Old school! print(f"Hello {variable}") # F-string, the cool kid on the block!

Too cool for school: f-strings

In Python 3.6 and above, f-strings provide a snappy way to include the value of expressions, variables and functions inside a string by using {} braces:

x = 5 y = 10 print(f"Adding {x} and {y} equals {x + y}. Quick maths!") # Using expression in f-string

With f-strings, you can also include expressions and functions directly in your string:

import math print(f"The square root of 25 is {math.sqrt(25)}") # Use functions in f-string

When you need a format: using .format()

The str.format() method is versatile, flexible, and nearly as readable as f-strings. The .format() method is your secret weapon when compatibility with Python 2 is necessary:

character = "Pythonista" print("Hello, {}. Prepare for trouble, and make it double!".format(character)) # Basic usage print("{2}, {1}, and {0}".format('a', 'b', 'c')) # Want to be backwards? No problem!

Hang on - there's more! Tweak the representation of objects inside the string. Say hello to padding, alignment, and other format specifiers:

print("|{:>10}|".format("text")) # > is for right alignment print("|{:^10}|".format("text")) # ^ is for center alignment print("|{:.5}|".format("xylophone")) # Precision in string print("|{:=+10}|".format((- 23))) # = forces the signed (+/-) to the leftmost position

Concatenation: going back to basics

When the string is static (does not change), you can simply use + to concatenate the string and variable:

salutation = "Dr." name = "Who" print(salutation + " " + name) # Doctor Who? Yes, that's me.

Remember, the str() function is your friend for converting non-string variables before concatenation:

wives_of_henry_viii = 6 print("Henry VIII had " + str(wives_of_henry_viii) + " wives. Talk about being indecisive!") # str() for the rescue

"%s" me up: printf-style

If you're a fan of C or feeling a bit nostalgic, Python supports printf-style string formatting using %. Although a bit old-fashioned, this method is still widely used:

name = "Waldo" print("Hello, %s! Good to see you. Took a while though." % name)

Tips & Tricks for polished output

For polished and clean printouts, keep these trade secrets at your fingertips:

Formatting floats

Use :.nf where n is a digit to control decimal precision in .format() and f-strings:

val = 12.3456789 print(f"Formatted number: {val:.2f}") # f-string way print("Formatted number: {:.2f}".format(val)) # .format() way

This will help with the annoying floating point precision hiccups that keep you up all night 🛌💤!

String quotes

Strings are enclosed in single (' ') or double (" ") quotes. Single quotes are useful to include double quotes within the string and vice versa:

print('Here is a "double quote" inside a string.') print("And here's 'single quote' inside a string!")

For exceptionally long strings involving both quotation forms, triple single or double quotes are your best friend!