国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python入門教程2. 字符串基本操作【運算、格式化輸出、常用函數】

2020-01-04 14:11:32
字體:
來源:轉載
供稿:網友

前面簡單介紹了Python基本運算,這里再來簡單講述一下Python字符串相關操作

1. 字符串表示方法

>>> "m.survivalescaperooms.com" #字符串使用單引號(')或雙引號(")表示'm.survivalescaperooms.com'>>> 'm.survivalescaperooms.com''m.survivalescaperooms.com'>>> "www."+"jb51"+".net" #字符串可以用“+”號連接'm.survivalescaperooms.com'>>> "#"*10 #字符串可以使用“*”來代表重復次數'##########'>>> "What's your name?" #單引號中可以直接使用雙引號,同理雙引號中也可以直接使用單引號"What's your name?">>> path = r"C:/newfile" #此處r開頭表示原始字符串,里面放置的內容都是原樣輸出>>> print(path)C:/newfile

2. 字符串運算

>>> str1 = "python test">>> "test" in str1 #這里in用來判斷元素是否在序列中True>>> len(str1) #這里len()函數求字符串長度11>>> max(str1)'y'>>> min(str1)' '

3. 字符串格式化輸出(這里重點講format函數)

>>> "I like %s" % "python" #使用%進行格式化輸出的經典表示方式'I like python'>>> dir(str) #列出字符串所有屬性與方法['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

① format(*args,**kwargs) 采用*args賦值

>>> str = "I like {1} and {2}" #這里{1}表示占位符(注意:這里得從{0}開始)>>> str.format("python","PHP")Traceback (most recent call last): File "<pyshell#5>", line 1, in <module>  str.format("python","PHP")IndexError: tuple index out of range>>> str = "I like {0} and {1}">>> str.format("python","PHP")'I like python and PHP'>>> "I like {1} and {0}".format("python","PHP")'I like PHP and python'>>> "I like {0:20} and {1:>20}".format("python","PHP")#{0:20}表示第一個位置占據20個字符,并且左對齊。{1:>20}表示第二個位置占據20個字符,且右對齊'I like python        and         PHP'>>> "I like {0:.2} and {1:^10.2}".format("python","PHP")#{0:.2}表示第一個位置截取2個字符,左對齊。{1:^10.2}表示第二個位置占據10個字符,且截取2個字符,^表示居中'I like py and   PH  '>>> "age: {0:4d} height: {1:6.2f}".format("32","178.55") #這里應該是數字,不能用引號,否則會被當作字符串而報錯!Traceback (most recent call last): File "<pyshell#0>", line 1, in <module>  "age: {0:4d} height: {1:6.2f}".format("32","178.55")ValueError: Unknown format code 'd' for object of type 'str'>>> "age: {0:4d} height: {1:8.2f}".format(32,178.5523154) #這里4d表示長度為4個字符的整數,右對齊。8.2f表示長度為8,保留2位小數的浮點數,右對齊。'age:  32 height:  178.55'

② format(*args,**kwargs) 采用**kwargs賦值

>>> "I like {str1} and {str2}".format(str1 = "python",str2 ="PHP")'I like python and PHP'>>> data = {"str1":"PHP","str2":"Python"}>>> "I like {str1} and {str2}".format(**data)'I like PHP and Python'

小結:對齊方式為:

< 左對齊
> 右對齊
^ 居中對齊

 

4. 字符串函數

>>> # isalpha()判斷字符串是否全部為字母>>> str1 = "I like python" #這里str1里面有空格>>> str1.isalpha()False>>> str2 = "pythonDemo" #這里為全部是字母>>> str2.isalpha()True>>> # split()分割字符串>>> smp = "I like Python">>> smp.split(" ")['I', 'like', 'Python']>>> # strip()去除字符串兩端空格 ,類似的,lstrip去除左側空格,rstrip去除右側空格>>> strDemo = "  python demo  ">>> strDemo.strip() #類似于php中的trim()函數'python demo'>>> "****python**".strip("*") #strip()函數還可刪除指定字符'python'

字符串常用函數【轉換、判斷】

 

split() 分割字符串
strip() 去除字符串兩端空格
upper() 轉大寫
lower() 轉小寫
capitalize() 首字母轉大寫
title() 轉換為標題模式(字符串中所有首字母大寫,其他字母小寫)
swapcase() 大寫轉小寫,小寫轉大寫(如:"I Like Python".swapcase() 得到i lIKE pYTHON)
isupper() 判斷字母是否全部為大寫
islower() 判斷字母是否全部為小寫
istitle() 判斷是否為標題模式(字符串中所有單詞首字母大寫,其他小寫)
isalpha() 判斷字符串是否全部為字母
isdigit() 判斷字符串是否全部為數字
isalnum() 判斷字符串是否僅包含字母與數字

 

>>> #字符串拼接(對于+不適用的情況下可以使用)>>> smp = "I like Python">>> c = smp.split(" ")>>> c = "I like python".split()>>> type(c) #這里檢測類型,可以看到c為列表<class 'list'>>>> "*".join(c)'I*like*Python'>>> " ".join(c)'I like Python'>>> " ".join(["I","Like","python"]) #這里可以直接使用列表作為join函數的參數'I Like python'>>> " ".join("abcd") #也可直接使用字符串作為join函數的參數'a b c d'>>> # count()函數統計指定字符串出現次數>>> "like python,learn python".count("python")2>>> # find()函數查找指定字符串出現位置>>> "python Demo".find("python")0>>> "python Demo".find("Demo")7>>> # replace(old,new)函數替換指定字符串(old)為新字符串(new)>>> "I like php".replace("php","python")'I like python'

簡單入門教程~

基本一看就懂~O(∩_∩)O~

未完待續~~歡迎討論!!


注:相關教程知識閱讀請移步到python教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 罗源县| 渭源县| 连城县| 沙湾县| 华蓥市| 鸡东县| 河东区| 贵定县| 射阳县| 西平县| 阜康市| 龙江县| 霞浦县| 微山县| 上饶县| 电白县| 宁津县| 曲沃县| 时尚| 读书| 溆浦县| 民权县| 青河县| 陕西省| 宾川县| 陇西县| 正安县| 澎湖县| 嘉荫县| 都昌县| 遂宁市| 恭城| 崇明县| 平湖市| 阳新县| 汤原县| 浦城县| 柳州市| 若尔盖县| 石家庄市| 兖州市|