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

首頁 > 編程 > Python > 正文

Python入門教程2. 字符串基本操作【運(yùn)算、格式化輸出、常用函數(shù)

2020-02-15 23:28:20
字體:
供稿:網(wǎng)友

前面簡(jiǎn)單介紹了Python基本運(yùn)算,這里再來簡(jiǎn)單講述一下Python字符串相關(guān)操作

1. 字符串表示方法

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

2. 字符串運(yùn)算

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

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

>>> "I like %s" % "python" #使用%進(jìn)行格式化輸出的經(jīng)典表示方式'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}表示第一個(gè)位置占據(jù)20個(gè)字符,并且左對(duì)齊。{1:>20}表示第二個(gè)位置占據(jù)20個(gè)字符,且右對(duì)齊'I like python        and         PHP'>>> "I like {0:.2} and {1:^10.2}".format("python","PHP")#{0:.2}表示第一個(gè)位置截取2個(gè)字符,左對(duì)齊。{1:^10.2}表示第二個(gè)位置占據(jù)10個(gè)字符,且截取2個(gè)字符,^表示居中'I like py and   PH  '>>> "age: {0:4d} height: {1:6.2f}".format("32","178.55") #這里應(yīng)該是數(shù)字,不能用引號(hào),否則會(huì)被當(dāng)作字符串而報(bào)錯(cuò)!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表示長(zhǎng)度為4個(gè)字符的整數(shù),右對(duì)齊。8.2f表示長(zhǎng)度為8,保留2位小數(shù)的浮點(diǎn)數(shù),右對(duì)齊。'age:  32 height:  178.55'            
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 海原县| 叙永县| 和林格尔县| 元朗区| 团风县| 庄河市| 江孜县| 玉环县| 瑞昌市| 任丘市| 龙山县| 津市市| 定西市| 延川县| 云阳县| 工布江达县| 武陟县| 湘潭市| 湖北省| 逊克县| 灵丘县| 德令哈市| 阳朔县| 崇礼县| 淮阳县| 德阳市| 黄平县| 关岭| 寿光市| 抚顺县| 乌拉特前旗| 科尔| 深泽县| 扎赉特旗| 通州市| 曲沃县| 平潭县| 沙河市| 东阿县| 永平县| 恭城|