I need to pass multiple variables in string concatenation

Can you explain if I need to pass a variable several times to concatenate strings.

For instance,

String1 = "Hello" String = "Good Morning" String2 = String + "%s, %s" % (String1, String1) 

My question is: how to pass String1 only once?

Is there a better way to do this?

+5
source share
1 answer

If you use the new str.format method, you can do:

 String2 = String + "{0}, {0}".format(String1) 

In fact, you always prefer str.format formatting over % in modern Python. The latter approach is pseudo-obsolete and will most likely be removed from a future version of the language.

+6
source

Source: https://habr.com/ru/post/1209395/


All Articles