Python: String

↑ top




string interpolation


without format

variable = 'World'
output = f'Hello {variable}!'

# Hello World


using format

template1 = 'Hello {}!'
output1 = template1.format('World')

template2 = 'Hello {variable}!'
output2 = template2.format(variable='World')

template3 = 'Hello {0}!'
output3 = template3.format('World')

# Hello World


↑ top




string concatenation


without format

variable = 'World'
output = f'Hello {variable}!'

# Hello World


using format

template1 = 'Hello {}!'
output1 = template1.format('World')

template2 = 'Hello {variable}!'
output2 = template2.format(variable='World')

template3 = 'Hello {0}!'
output3 = template3.format('World')

# Hello World


↑ top