Python Merge Multiple Strings
In Python, merging multiple strings can be achieved in several ways, including using the plus sign +、join() method or formatted strings. Below we will demonstrate how to merge multiple strings through a simple example.
Example
Define multiple strings
str1
=
"Hello"
str2
=
"World"
str3
=
"!"
Merge strings using the plus sign
combined_str1
=
str1 +
" "
- str2 + str3
Merge strings using the join() method
combined_str2
=
" "
.
join
(
[
str1
,
str2
]
)
- str3
Merge using formatted strings
combined_str3
=
f
"{str1} {str2}{str3}"
Output result
(
combined_str1
)
(
combined_str2
)
(
combined_str3
)
Code analysis:
str1,str2,str3are the three strings to be merged.combined_str1Use the plus sign+to concatenate the strings, adding a space in between.combined_str2Usejoin()method joins the elements in the string list together, and then addsstr3。combined_str3Use formatted stringsf""to embed variables directly into the string.
Output result:
Hello World!
Hello World!
Hello World!
Python3 Examples