Python string
{ 拼接字符串 使用“+”可以对多个字符串进行拼接 语法格式: str1 + str2 ? 1 2 3 4 >>> str1 = "aaa" >>> str2 = "bbb" >>> print (str1 + str2) aaabbb 需要注意的是字符串不允许直接与其他类型进行拼接,例如 ? 1 2 3 4 5 6 7 >>> num = 100 >>> str1 = "hello" >>> print (str1 + num) Traceback (most recent call last): File "<pyshell#5>" , line 1 , in <module> print (str1 + num) TypeError: can only concatenate str ( not "int" ) to str 上面这种情况我们可以将num转换为字符串再进行拼接 ? 1 2 3 4 >>> num = 100 >>> str1 = "hello" >>> print (str1 + str (num)) hello100 这样就不会报错了 计算字符串的长度 在Python中使用len()函数来计算字符串的长度 语法格式: len(string) ? 1 2 3 4 5 6 7 8 9 >>> str1 = "hello" >>> len (str1) 5 >>>