# -*- coding: utf-8 -*-
# @Time : 2019/11/7 10:18
# @Author : John
# @Email : 2398344557@qq.com
# @File : 字符串.py
# @Software: PyCharm
import string
words = string.ascii_lowercase[:9] # 切片輸出前9個小寫的ascii碼
print(words)
# —— abcdefghi
print(words[3])
# —— d
print(words[2:4])
# —— cd
print(words[:]) # 默認返回整個字符串暇仲,但只是淺復(fù)制
# —— abcdefghi
print(words[::]) # 默認返回整個字符串务甥,但只是淺復(fù)制
# —— abcdefghi
print(words[-2:])
# —— hi
print(words[-2:2]) # 啥都沒赎离,空值
#
print(words[::-1])
# —— ihgfedcba
- 字符串中四個比較重要的操作方法(內(nèi)置函數(shù))
# ****strip()炸客、split()泊柬、join()、replace()****
- 大小寫處理:upper()和lower()
# 1. upper()和lower()
# 常用指數(shù):***
str1 = 'www.NEUEDU.com'
str2 = str1.upper() # 全部大寫(已是大寫的會直接輸出無需處理)
print(str2)
# —— WWW.NEUEDU.COM
str3 = str1.lower() # 全部小寫(已是小寫的會直接輸出無需處理)
print(str3)
# —— www.neuedu.com
- 判斷開頭結(jié)尾:startswith()和startswith()
# 2. startswith(prefix, start, end)和startswith(suffix, start, end)
# 常用指數(shù):***
str1 = 'www.eeeNEUEDU.com'
print(str1.startswith('www')) # 判斷是否以www開頭
# —— True
print(str1.endswith('.com')) # 判斷是否以.com結(jié)尾
# —— True
print(str1.startswith('eee', 4, 6)) # 左閉右開
# —— False
- 查找元素:find()和index()
# 3. 查找元素:find(sub, _start, _end)和index(sub, _start, _end)
# 常用指數(shù):****
str1 = 'www.eeeNEUEDU.com'
print(str1.find('h')) # 尋找字符串中的h元素皆的,找不到時返回負一
# —— -1
print(str1.find('w')) # 從左邊開始,獲取指定元素首次出現(xiàn)的下標
# —— 0
print(str1.find('N'))
# —— 7
print(str1.find('NEUEDU')) # 不會報錯怨酝,但只會返回輸入字符串中首字母首次出現(xiàn)的下標
# —— 7
print(str1.rfind('w')) # 從右邊開始,獲取指定元素首次出現(xiàn)的下標
# —— 2
# ------------------------
# print(str1.index('h')) # index找不到這個子串時會報錯
# —— ValueError: substring not found
print(str1.index('w'))
# —— 0
print(str1.find('N'))
# —— 7
print(str1.index('NEUEDU')) # 不會報錯那先,但只會返回輸入字符串中首字母首次出現(xiàn)的下標
# —— 7
print(str1.rindex('w')) # 從右邊開始农猬,獲取指定元素首次出現(xiàn)的下標
# —— 2
- 清除占位符:strip()
# 4. strip(chars):默認取出字符前后兩端的空格、換行售淡、tab
# # 常用指數(shù):*****
str1 = ' \n .com \t ' # 16=4+2+1+4+1+4
print(len(str1))
# —— 16
print(len(str1.strip())) # 剩.com
# —— 4
str2 = 'aabbccddff'
str2 = str2.strip('aa') # 刪除指定的首字符串(指定為左端)
print(str2)
# —— bbccddff
str3 = 'aabbccddff'
str3 = str3.lstrip('ff') # 刪除指定的首字符串(指定為左端)斤葱,沒有時輸出原字符串
print(str3)
# —— aabbccddff
str4 = 'aabbccddff'
str4 = str4.rstrip('ff') # 刪除指定的首字符串(指定為左端)
print(str4)
# —— aabbccdd
- 分隔成列表:split()和splitlines()
# 5. split(sep, maxsplit):把字符串分隔成列表,默認是以空格進行分割
# 常用指數(shù):*****
str1 = 'life is short, use Pyhton.'
print(str1.split()) # 英文版的jieba分詞揖闸,默認對空格進行分割
# —— ['life', 'is', 'short,', 'use', 'Pyhton.']
print(str1.split(',')) # 指定對逗號進行分割
# —— ['life is short', ' use Pyhton.']
str2 = 'life;is;short,;use;Pyhton,; '
print(str2.split(';', 3)) # 指定分割多少個
# —— ['life', 'is', 'short,', 'use;Pyhton,; ']
print(str2.split(';', 4)) # 指定分割多少個
# —— ['life', 'is', 'short,', 'use', 'Pyhton,; ']
print(str2.split(';', 5)) # 指定分割多少個
# —— ['life', 'is', 'short,', 'use', 'Pyhton,', ' ']
print(str2.split(';', 3)[0])
# —— life
# 14.splitlines():按照行分割揍堕,返回包含按照行分割的元素列表(和第五合并)
# 常用指數(shù):**
str2 = 'www.\nwww.NEUEDU.com'
print(str2)
# —— www.NEUEDU.com
print(str2.splitlines())
# —— ['www.', 'www.NEUEDU.com']
- 把列表轉(zhuǎn)換成字符串:join()
# 6. join(iterable):把列表轉(zhuǎn)換成字符串
# 常用指數(shù):*****
str1 = 'life is short, use Pyhton.'
list1 = str1.split() # list1里面元素必須全是字符串
print(list1)
# —— ['life', 'is', 'short,', 'use', 'Pyhton.']
print(''.join(list1)) # 無縫連接
# —— lifeisshort,usePyhton.
print(' '.join(list1)) # 空格隔開
# —— life is short, use Pyhton.
print('_'.join(list1))
# —— life_is_short,_use_Pyhton.
print('//'.join(list1))
# —— life//is//short,//use//Pyhton.
- is系列
# 7. is系列
# 常用指數(shù):***
name = 'Xuebi520'
print(name.isdigit()) # 判斷所有的字符串是否為數(shù)字
# —— False
print(name.isalpha()) # 判斷所有的字符串是否為字母
# —— False
print(name.isalnum()) # 判斷所有的字符串是否為數(shù)字或者是字母
# —— True
print(name.islower()) # 判斷所有的字符串是否為小寫字母
# —— False
print(name.isupper()) # 判斷所有的字符串是否為大寫字母
# —— False
print(name.istitle()) # 判斷所有的字符串是否為大寫字母開頭
# —— True
print(name.isspace()) # 判斷所有的字符串是否為空白字符
# —— False
- 計算某個字符出現(xiàn)的次數(shù):count()
# 8. count(x, _start, _end):計算某個字符出現(xiàn)的次數(shù)
# 常用指數(shù):****
name = 'Xuebi7533'
print(name.count('3'))
# —— 2
- 替換指定的字符:replace()
# 9. replace(old, new, count):替換指定的字符
# 常用指數(shù):*****
name = '7533Xuebi7533'
name2 = name.replace('7533', '')
print(name2)
# —— Xuebi
name3 = name.replace('7533', '', 1) # 替換第一次出現(xiàn)的
print(name3)
# —— Xuebi7533
- 首字母大寫:capitalize()
# 10. capitalize():首字母大寫
# 常用指數(shù):***
name = 'xuebi7533'
print(name.capitalize())
# —— xuebi7533
- 左中右對齊: ljuest()、center()汤纸、rjuest()
# 11. center(width, fillchar):將字符串居中
# 常用指數(shù):**
# 參數(shù)可以設(shè)置字符串總長度衩茸,可以用*進行填充
name = 'xuebi7533'
print(name.center(20))
# —— xuebi7533
print(name.center(20, '*'))
# —— *****xuebi7533******
print(len(name.center(20, '*')))
# —— 20
name2 = 'xuebi'
print(name2.center(20))
# —— xuebi
print(name2.center(20, '*'))
# —— *******xuebi********
print(len(name2.center(20, '*')))
# —— 20
# 15. ljust()和rjust() (和第11合并)
# 常用指數(shù):**
str1 = 'neuedu'
print(len(str1))
# —— 6
print(str1.ljust(20, '*')) # 左對齊
# —— neuedu**************
print(len(str1.ljust(20)))
# —— 20
print(str1.rjust(20, '*')) # 右對齊
# —— **************neuedu
print(len(str1.rjust(20)))
# —— 20
- 非字母隔開的每個單詞的首字母大寫:title()
# 12. title():非字母隔開的每個單詞的首字母大寫
# 常用指數(shù):*
name = 'xue bi, &w7533t=f'
print(name.title())
# —— Xue Bi, &W7533T=F
- 分割三部分:partition()
# 13. partition(sep):將一個字符串分割成三部分(前、中贮泞、后)
# 常用指數(shù):**
str1 = 'www.www.NEUEDU.com'
print(str1.partition('www')) # 返回的是元組
# —— ('', 'www', '.www.NEUEDU.com')
print(str1.rpartition('www'))
# —— ('www.', 'www', '.NEUEDU.com')