A string of splicing
a. plus sign (+) splice
str = “Hello” + “ “ + “python” + “!”
# ----------------------------------------
mosaic # string: +, join function, format
# ----------------------------------------
# + (plus ) the two strings are connected together into a new string
str1 = "the Hello"
str2 = "Python"
Str3 = "!"
str_new = str1 + "" + + str2 Str3
Print (str_new)
# May also be used the addition assignment operator (+ =) connected
str_new + = "^ - ^"
Print (str_new)
b.join stitching function
str = sep.join(seq)
sep: separator; seq: sequence of strings to be connected
# Join function 'sep'.join (SEQ)
# On Sep: separator; seq: sequence string to connect
On Sep = ""
str4 = sep.join ( "the Hello")
Print (str4)
seq = ['10', '20', '30', '40']
str5 = sep.join(seq)
print(str5)
c. String Format
1. string operator%
2. Use str.format () function (recommended)
# String Format:%, str.format ()
#% to string format string operator
major_version. 3 =
minor_version = 6.400
STR6 = "%% use formatted output:.% S% s% d %. 1F! "% (str1, str2, major_version, minor_version)
Print (STR6)
=. 1 NUM
STR7 = "only if the argument% d parentheses may be omitted" NUM%
Print (STR7)
# char = 'A'
char = 65 # Output ASCII code corresponding to characters A: 97
Str8 = "%% for C formatting characters and their ASCII code:% c "char%
Print (Str8)
# Can be specified length of the output digital data
# -, alignment marks left, right alignment default
# 0, data indicative of insufficient length, filled with zeros. The default space filled
str9 = "total score is the total score% 5s% 3d \ n% -5s is% 03d"% ( "Li Yu," 99, "Liu", 96)
Print (STR9)
# +, It represents the sign to be output, the default output only negative
str10 = "temperature of Guangzhou% + d, the temperature was Harbin + D%"% (18 is, -18)
Print (str10)
Use str.format # () format string
# {} and the characters inside (referred to as field formatted) will be format () parameter substitution in
# 1, the output parameter (corresponding number automatically according to the default order )
Print ( "major search engines is: {} and {} \ n" .format ( 'Baidu', 'Google'))
# 2, in the format specified location parameters (manual numbering) within a digital parentheses
print ( "major search engines are: {0} and {1}". Format ( 'Baidu', 'the Google'))
print ( " major search engines is: {1} and {0} \ n {0} I use a little more ".format ( 'Baidu', 'Google'))
# 3, to the parameter definition keyword, and in combination with other
# Note: You can not mix mode automatic numbering and a manual numbering used together. And position parameters can not be at the back of the key parameters.
print ( "search engines: {0}, {1} and {sou_gou}, etc." .format ( 'Baidu', 'Google', sou_gou = ' search dogs'))
print ( "search engines: {}, { sou_gou} {} and the like ".format ( 'Baidu', 'Google', sou_gou = ' search dogs'))
# Print ( "search engines: {0}, {1} and {2}, and so" .format ( 'Baidu', 'Google', sou_gou = 'search dogs'))
format_str1 = "two digits after decimal places: {} :.. 2F." the format (3.1415926, 8.888, PI = 3.1415926)
Print (format_str1)
format_str2 = "after the decimal with no decimal places, and retention sign: {: .0f} +. "the format (3.1415926)
Print (format_str2)
# <, Left justified, right side of the filling space;>, right, left with spaces
format_str3 = "Default Left: {: 20S} the Python!" The format ( "the Hello").
Print (format_str3)
format_str4 = "> right justified: !. "the format: {> 20S} the Python (" the Hello ")
Print (format_str4)
format_str5 =" default fill spaces to fill -: {: -> 20S}!. "the format the Python (" the Hello ")
Print (format_str5 )
format_str6 = "designating data length of 3, right, is not enough left to fill 0: {: 0> 3D}." the format (. 9)
Print (format_str6)
format_str7 = "designating data length of 3, left-justified, is not enough to the right to fill 0: {: 0 <3D}. "the format (. 9)
Print (format_str7)
String formatting symbols:
Second, the escape string
Escape symbol string
Third, the character string taken
Use str [x], str [x: y] intercept character string in the specified range, comprising x, but does not contain y.
# ----------------------------------------
# 字符串的截取:[]、[:]
# ----------------------------------------
string = "字符串的截取操作!"
# str[i] 表示截取第几个字符,字符索引值从0开始
print("string[1]:", string[1])
# str[x:y] x表示开始截取的索引值,y表示停止截取的索引值(包含x,但不包含y)
print('string[2:5]:', string[2:5])
# 如果省略开始索引值,就从第一个字符到结尾索引值
print('string[:5]:', string[:5])
# 如果省略结尾索引值,就从开始索引值到最后一个字符
print('string[2:]:', string[2:])
# 更新字符串
# string[3] = "被"
string = string[:3] + "被" + string[4:]
print(string)
# 使用in和not in判断某个字符或字符串是否存在于另一个字符串中
sub_string = "截取"
if sub_string in string:
print("in")
else:
print("not in")
sub_string = "是"
if sub_string not in string:
print("not in")
else:
print("in")
四、字符串的常用函数
count():统计字串里某个字符出现的次数
len():返回字符串的长度
find():检测字符串中是否包含子字符串
replace():把字符串中的旧字符串替换为新字符串
lower():转换字符串中的所有大写字母为小写
upper():转换字符串中的所有小写字母为大写
swapcase():对字符串中的大小写字母进行相互转换
strip():删除字符串左右两侧的空格或指定的字符
isalnum():检测字符串中的字符是否全部由字母或数字组成
# ----------------------------------------
# 字符串的常用函数
# ----------------------------------------
# count() 用于统计字符串里某个字符出现的次数
string = "Hello python!"
sub_str = 'o'
print("字符o出现的次数为:", string.count(sub_str))
# len()返回字符串的长度
print("字符o出现的次数为:", string.count(sub_str, 5, len(string)))
# find() 检测字符串中是否包含子字符串。如果包含,返回开始的索引值;否则返回-1。
sub_str1 = "py"
sub_str2 = "pn"
print("查找子串py,返回索引值:", string.find(sub_str1))
print("查找子串pn,返回索引值:", string.find(sub_str2, 2))
print("查找子串py,返回索引值:", string.find(sub_str1, 3, 8))
# index()与find()函数的用法相同,只是当字符串中没有包含指定的子串时,会报一个异常,而不是返回-1。
# replace() 把字符串中的旧字符串替换为新字符串
print(string.replace("python", "word")) # 默认是进行全部替换
print(string.replace("o", "w", 1)) # 也可以指定替换的最大次数
# swapcase() 对字符串中的大小写字母进行相互转换
print("swapcase:", string.swapcase())
# lower() 转换字符串中所有大写字符为小写
print("lower:", string.lower())
# upper() 转换字符串中所有小写字母为大写
print("upper:", string.upper())
# strip() 删除字符串左右两侧的空格或指定的字符
string2 = "------ch02_基础语法--------"
print("strip:", string2.strip('-'))
string1 = "Python3"
# isalnum() 如果字符串至少有一个字符,并且所有字符都是字母或数字则返回True,否则返回False。
print("isalnum:", string1.isalnum())
# isalpha() 如果字符串至少有一个字符,并且所有字符都是字母则返回True,否则返回False。
print("isalpha:", string1.isalpha())
# isdigit() 如果字符串只包含数字则返回True,否则返回False。
print("isdigit:", string1.isdigit())
# title() 返回一个标题化的字符串(即所有单词以大写开头,其余字母均为小写)
print(string.title())
# center() 返回一个指定宽度并且居中显示的字符串,并在左右两侧填充指定的字符,默认为空格
print("center:", string.center(40, '-'))