形式語言理論(特別是編程語言),字串串接(Concatenation ),又稱字串相加、連接、序連、串連、相連,指將兩個字串的首尾相接的操作
mystring1 = "Hello, everyone !\\n"
mystring2 = "My name is Albert "
result = mystring1 + mystring2
print(result)
output -------------------------------------------------------------------
[Running] python -u "d:\\2022_python_全攻略\\15. String Concatenation"
Hello everyone!
My name is Albert
#string + number Error
mystring1 = "Hello team everyone"
myluckynumber = 777
result = mystring1 + myluckynumber
print(result)
--------------------------------------------------
[Running] python -u "d:\\2022_python_全攻略\\15. String Concatenation"
Traceback (most recent call last):
File "d:\\2022_python_\\u5168\\u653b\\u7565\\15. String Concatenation", line 9, in <module>
result = mystring1 + myluckynumber
***TypeError: can only concatenate str (not "int") to str***
#string is immutable
mystring1 = "Hello"
mystring1[0] = "h"
print(mystring1)
---------------------------------------------------------
[Running] python -u "d:\\2022_python_全攻略\\15. String Concatenation"
Traceback (most recent call last):
File "d:\\2022_python_\\u5168\\u653b\\u7565\\15. String Concatenation", line 14, in <module>
mystring1[0] = "h"
***TypeError: 'str' object does not support item assignment***