Skip to main content
added 10 characters in body
Source Link
desertnaut
  • 60.8k
  • 32
  • 155
  • 183

Depends on what you are trying to do. If you are formatting a variable into a string to print, e.g. you want the output to be:

Hello, Bob

Hello, Bob

Given the name Bob, you'd want to us %s. print("Hello, %s" % my_variable)%s. print("Hello, %s" % my_variable) It's efficient, and it works with all data-types (so you don't have to do str(my_variable)str(my_variable) like you do with "a" + str(5)"a" + str(5)).

Depends on what you are trying to do. If you are formatting a variable into a string to print, e.g. you want the output to be:

Hello, Bob

Given the name Bob, you'd want to us %s. print("Hello, %s" % my_variable) It's efficient, and it works with all data-types (so you don't have to do str(my_variable) like you do with "a" + str(5)).

Depends on what you are trying to do. If you are formatting a variable into a string to print, e.g. you want the output to be:

Hello, Bob

Given the name Bob, you'd want to us %s. print("Hello, %s" % my_variable) It's efficient, and it works with all data-types (so you don't have to do str(my_variable) like you do with "a" + str(5)).

Source Link
Hchap
  • 58
  • 10

Depends on what you are trying to do. If you are formatting a variable into a string to print, e.g. you want the output to be:

Hello, Bob

Given the name Bob, you'd want to us %s. print("Hello, %s" % my_variable) It's efficient, and it works with all data-types (so you don't have to do str(my_variable) like you do with "a" + str(5)).