Python Concat String

August 9, 2017
Format string, join list or append string.

Format String

If you want to join a fixed number of variables in a fixed format, string.format is your best bet.

value = u"Desmond Lua 赖忠景"
field = "Name"
# use 'u' to properly handle unicode formatting 
output = u"{0}: {1}".format(field, value)

You can also use + operator to concat string.

output = field + ": " + value

Join list

If you have an array of strings which you would like to join together, string.join is convinient and fast.

values = ["Please", "join", "these", "words", "together"]
output = " ".join(values)

Manually append string

If you want to manually append string without using a list, you can use += operator.

output = "Please"
output += "Join"
output += "Us"
output += "Together"
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.