concatenate string and variable python code example

Example 1: how to concat join 2 strings in python

str1 = “Hello”
str2 = “World”
str1 + str2

Example 2: python concatenate strings

x = ‘apples’
y = ‘lemons’
z = “In the basket are %s and %s” % (x,y)

Example 3: combine two strings python

>>> orig_string = 'Hello'
>>> orig_string + ', world'
'Hello, world'
>>> orig_string
'Hello'
>>> full_sentence = orig_string + ', world'
>>> full_sentence
'Hello, world'

Example 4: python merge strings

my_list = ['a', 'b', 'c', 'd']
my_string = ','.join(my_list)
# Output = 'a,b,c,d'

Example 5: python concatenate strings

print{0} {1} is {2} years old.format(fname, lname, age)