What is the Python 3 equivalent of %s in strings?

Gavin picture Gavin · Sep 25, 2014 · Viewed 24.2k times · Source

I've made some basic progress in python before, nothing more than command land algebra calculators to do math homework, using user-defined functions, inputs, and basic stuff. I've since taken the Python 2 course that codeacademy has, and I'm finding no equivalent of using % and %s for PY3.

I've narrowed it down to having some relation to format() , but that's as far as I could find on Google.

As a beginner, I'd really appreciate a watered down explanation to how to translate this into Python 3:

str1 = "Bob,"
str2 = "Marcey."
print "Hello %s hello %s" % (str1, str2)

EDIT: Also, I'm aware that print("Hello " + str1 + "hello " + str2) would work.

Answer

Ignacio Vazquez-Abrams picture Ignacio Vazquez-Abrams · Sep 25, 2014

str.__mod__() continues to work in 3.x, but the new way of performing string formatting using str.format() is described in PEP 3101, and has subsequently been backported to recent versions of 2.x.

print("Hello %s hello %s" % (str1, str2))

print("Hello {} hello {}".format(str1, str2))