Concatenating (joining) strings in Python is very simple comparing to other programming languages.
You can write two strings directly next to each other in the following format:
string_id = "str_1" "str_2"
string_id represents the string variable name after concatenation, str_1 and str_2 are the contents
of the string to be concatenated. Using this notation, Python will automatically join two strings
[Example] Concatenate strings in the form of continuous writing:
In many application scenarios, we need to join strings and numbers together, and
Python does not allow direct joining of numbers and strings, so we must first convert numbers
to strings. Numbers can be converted to strings with the help of the str() and repr() functions,
which are used in the format:
obj represents the object to be converted. It can be numbers, lists, tuples, dictionaries,
and other types of data.
Look at the following code:
name = "Alice"
age = 18
data = name + " is " + str(age) + " years old."
The output is:
Alice is 18 years old.
The difference between str() and repr() in Python
Although str () and repr () can both convert numbers to strings, there are differences between them:
str() is used to convert data into a human-readable string form.
repr() is used to convert the data into a string format (Python expression) suitable for reading by
the interpreter, suitable for use during development and debugging; if there is no equivalent
syntax, a SyntaxError exception occurs.
In this example, a is itself a string, but we still converted it using str() and repr(). It can be
seen from the running results that str() retains the original appearance of the string, and repr()
uses quotation marks to surround the string. This is the Python string expression form.
In addition, when an expression (variable, addition, subtraction, multiplication, division, logical
operation, etc.) is entered in the Python interactive programming environment, Python automatically
uses the repr () function to process the expression.