Python string processing method

Three forms of quotation marks can be used to construct a string. If the content of the string does not contain any quotation marks, then single quotation marks, double quotation marks and triple quotation marks can be used; show the following examples:

# 单引号构造字符串
string1 = '"commentTime":"2018-01-26 08:59:30","content":"包装良心!馅料新鲜!还会回购"'
# 双引号构造字符串
string2 = "ymd:'2017-01-01',bWendu:'5℃',yWendu:'-3℃',tianqi:'霾~晴',fengxiang:'南风',aqiInfo:'严重污染'"
# 三引号构造字符串
string3 = ''''nickName':"美美",'content':"环境不错,服务态度超好,就是有点小贵",'createTimestring':"2017-09-30"'''
string4 = '''据了解,持续降雪造成安徽部分地区农房倒损、种植养殖业大棚损毁,
其中合肥、马鞍山、铜陵3市倒塌农房8间、紧急转移安置8人。'''
print(string1)
print(string2)
print(string3)
print(string4)

If the content of the string contains only double quotation marks, similar to the variable string1, then only single quotation marks or double quotation marks can be used to construct the string; if the string content only contains single quotation marks, similar to the form of the variable string2, only double quotation marks or Triple quotation marks complete the creation of the string; if the content of the string includes single quotation marks and double quotation marks, only triple quotation marks can be used to construct the string. Therefore, triple quotation marks are the most applicable string construction method, and triple quotation marks allow the wrapping of long strings, which cannot be achieved by the other two quotation marks, as shown in the variable string4.

Common methods of string
string[start:end:step] #字符串的切片
string.split #字符串的分割
string.strip #删除首尾空白
string.rstrip #删除字符串右边空白
string.lstrip #删除字符串左边空白
string.index #返回子串首次出现的位置
string.find #返回子串首次出现的位置(找不到返回-1)
string.startswith #字符串是否以什么开头
string.endswith #字符串是否以什么结尾
string.format #字符串的格式化处理
string.replace #字符串的替换
sep.join #将可迭代对象按sep分割符拼接为字符串
string.count #对字符串的子串计数

Let's learn the above code through a small example:

# 获取身份证号码中的出生日期
print('123456198901017890'[6:14])
# 将手机号中的中间四位替换为四颗星
tel = '13612345678'
print(tel.replace(tel[3:7],'****'))
# 将邮箱按@符分隔开
print('[email protected]'.split('@'))
# 将Python的每个字母用减号连接
print('-'.join('Python'))
# 删除"  今天星期日  "的首尾空白
print("  今天星期日  ".strip())
# 删除"  今天星期日  "的左边空白
print("  今天星期日  ".lstrip())
# 删除"  今天星期日  "的右边空白
print("  今天星期日  ".rstrip())
# 计算子串“中国”在字符串中的个数
string5 = '中国方案引领世界前行,展现了中国应势而为、勇于担当的大国引领作用!'
print(string5.count('中国'))
# 查询"Python"单词所在的位置
string6 = '我是一名Python用户,Python给我的工作带来了很多便捷。'
print(string6.index('Python'))
print(string6.find('Python'))
# 字符串是否以“2018年”开头
string7 = '2017年匆匆走过,迎来崭新的2018年'
print(string7.startswith('2018年'))
# 字符串是否以“2018年”年结尾
print(string7.endswith('2018年'))
# 字符串的格式化处理
print('尊敬的{}先生,您的话费余额为{:.2f},请及时充值,以免影响正常通话!'.format('刘',8.669))
{
    
    :.2f} #保存小数后两位

It should be noted that the index and find methods of the string can only return the position where the substring is first found. If the substring is not found in the original string, for the index method, an error message is returned, but the find method returns the value -1, no error is reported.

Guess you like

Origin blog.csdn.net/m0_46445293/article/details/114886781