python print variable in string code example

Example 1: how to print variables in a string python

name = "Steven"

# Using f-strings
print(f"Hi, {name}!")

# Using format()
print("Hi, {}!".format(name))

Example 2: print string and variable python

x = input("Enter the first number: ")
y = input("Enter the second number: ")
z = int(x)+int(y)
#Just use a comma
print("The sum of the numbers you entered =",z)

Example 3: python print string and variable

print "If there was a birth every 7 seconds, there would be: {} births".format(births)

Example 4: print variable string python

print("If there was a birth every 7 seconds, there would be: ", births, "births")

Example 5: python print variables and string

# This prints out "John is 23 years old."
name = "John"
age = 23
print("%s is %d years old." % (name, age))
# note: the letter is based on the datatype. %s = string, %d = decimal

Example 6: python print variable

name = "Bob"
print(f"hello {name}!")

Tags:

Misc Example