日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python基础教程之第3章 使用字符串

發布時間:2024/7/19 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python基础教程之第3章 使用字符串 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. #3.1基本字符串操作 >>> website = 'http://www.python.org' >>> website[-3:]='com'Traceback (most recent call last):File "<pyshell#1>", line 1, in <module>website[-3:]='com' TypeError: 'str' object does not support item assignment #3.2 字符串格式化:精簡版 >>> format = "Hello, %s. %s is enough for ya?" >>> values = ('world', 'Hot') >>> print format % values Hello, world. Hot is enough for ya? >>> format = "Pi with three decimals: %.3f" >>> from math import pi >>> print format % pi Pi with three decimals: 3.142 #模板字符串 >>> from string import Template >>> s = Template('$x, glorious $x!') >>> s.substitute(x='slurm') 'slurm, glorious slurm!' >>> s = Template("It's ${x}tastic!") >>> s.substitute(x='slurm') "It's slurmtastic!" >>> s = Template("Make $$ selling $x!") >>> s.substitute(x='slurm') 'Make $ selling slurm!' >>> s = Template('A $thing must never $action.') >>> d={} >>> d['thing']='gentleman' >>> d['action']='show his socks' >>> s.substitute(d) 'A gentleman must never show his socks.' >>> #saft_substitute不會因缺少值或不對使用$字符而出錯. >>> '%s plus %s equlas %s' % (1,1,2) '1 plus 1 equlas 2' >>> '%s plus %s equlas %s' % 1,1,2Traceback (most recent call last):File "<pyshell#24>", line 1, in <module>'%s plus %s equlas %s' % 1,1,2 TypeError: not enough arguments for format string >>> '%s plus %s equlas %s' % 1,1,2 # Lacks parentheses!Traceback (most recent call last):File "<pyshell#25>", line 1, in <module>'%s plus %s equlas %s' % 1,1,2 # Lacks parentheses! TypeError: not enough arguments for format string #3.3 字符串格式化:完整版 #3.3.1簡單轉換 >>> 'Price of eggs: $%d' % 42 'Price of eggs: $42' >>> 'Hexadecimal price of eggs: %x' % 42 'Hexadecimal price of eggs: 2a' >>> from math import pi >>> 'Pi: %f...' % pi 'Pi: 3.141593...' >>> 'Very inexact estimate of pi: %i' % pi 'Very inexact estimate of pi: 3' >>> 'Using str: %s' % 42L 'Using str: 42' >>> 'Using repr: %r' % 42L 'Using repr: 42L' #3.3.2字段寬度和精度 >>> '%10f' % pi ' 3.141593' >>> '%10.2f' % pi ' 3.14' >>> '%.2f' % pi '3.14' >>> '%.5s' % 'Guido van Rossum' 'Guido' >>> '%.*s' % (5, 'Guido van Rossum') 'Guido' #3.3.3符號, 對齊和0填充 >>> '%010.2f' % pi '0000003.14' >>> 010 8 >>> '%-10.2f' % pi '3.14 ' >>> print ('% 5d' % 10) + '\n' + ('% 5d' % -10)F SyntaxError: invalid syntax >>> print ('% 5d' % 10) + '\n' + ('% 5d' % -10)10-10 >>> print ('% 5d' % 10) + '\n' + ('% 5d' % -10)F SyntaxError: invalid syntax >>> print ('% 5d' % 10) + '\n' + ('% 5d' % -10)10-10 >>> print ('%+5d' % 10) + '\n' + ('%+5d' % -10)+10-10#代碼清單3-1 字符串格式化演示樣例 #3.4字符串方法 #string模塊還包含一些不能作為字符串方式使用的常量和函數 >>> import string >>> string.digits '0123456789' >>> string.letters 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' >>> string.lowercase 'abcdefghijklmnopqrstuvwxyz' >>> string.uppercase 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' >>> string.punctuation '!"#$%&\'()*+,-./:;<=>?

@[\\]^_`{|}~' >>> string.printable '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c' >>> string.ascii_letters 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' #3.4.1 find >>> 'With a moo-moo here. and a moo-moo there'.find('moo') 7 >>> title = "Monty Python's Flying Circus" >>> title.find('Monty') 0 >>> title.find('Python') 6 >>> titie.find('Flying') Traceback (most recent call last): File "<pyshell#59>", line 1, in <module> titie.find('Flying') NameError: name 'titie' is not defined >>> title.find('Flying') 15 >>> title.find('Zirquss') -1 >>> subject = '$$$ Get rich now!!! $$$' >>> subject.find('$$$') 0 >>> subject.find('$$$', 1) 20 >>> subject.find('!!!') 16 >>> subject.find('!!!', 0, 16) -1 #3.4.2 join >>> seq = [1,2,3,4,5] >>> sep = '+' >>> sep.join(seq) Traceback (most recent call last): File "<pyshell#69>", line 1, in <module> sep.join(seq) TypeError: sequence item 0: expected string, int found >>> seq = ['1','2','3','4','5'] >>> sep.join(seq) '1+2+3+4+5' >>> dirs = '','usr','bin','env' >>> '/'.join(dirs) '/usr/bin/env' >>> print 'C:' + '\\'.join(dirs) C:\usr\bin\env #3.4.3 lower >>> 'Trondheim Hammer Dance'.lower() 'trondheim hammer dance' >>> if 'Gumby' in ['gumby','smith','jones']: print 'Found it!' >>> name='Gumby' >>> names=['gumby','smith','jones'] >>> if name.lower() in names: print 'Found it!' Found it! #標題轉換 >>> "that's all folks".title() "That'S All Folks" >>> import string >>> string.capwords("that's all, folks") "That's All, Folks" #3.4.4 replace >>> 'This is a test'.replace('is','eez') 'Theez eez a test' #3.4.5 split >>> '1+2+3+4+5'.split('+') ['1', '2', '3', '4', '5'] >>> '/usr/bin/env'.split('/') ['', 'usr', 'bin', 'env'] >>> 'Using the default'.split() ['Using', 'the', 'default'] #3.4.6 strip 相當于Java 中的 String.trim() >>> ' internal whitespace is kept '.strip() 'internal whitespace is kept' >>> names = ['gumby','smith','jones'] >>> name = 'gumby' >>> if name in names: print 'Found it!' Found it! >>> '*** SPAM * for * everyone!!! ***'.strip(' *!') 'SPAM * for * everyone' #3.4.7 translate >>> from string import maketrans >>> table = maketrans('cs', 'kz') >>> len(table) 256 >>> table[97:123] 'abkdefghijklmnopqrztuvwxyz' >>> maketrans('','')[97:123] 'abcdefghijklmnopqrstuvwxyz' >>> table = maketrans('cs','kz') >>> 'this is an incredible test'.translate(table) 'thiz iz an inkredible tezt' >>> 'this is an incredible test'.translate(table,' ') 'thizizaninkredibletezt' #非英語字符串問題 table = maketrans('X','x') word = 'Xxx' print word.translate(table).lower() print u'Xxx'.lower() #小結 #本章介紹了字符串的兩種很重要的使用方式 #字符串格式化: 求模操作符(%)能夠用來將其它值轉換為包含轉換標志的字符串,比如%s. 它還能用來對值進行不同方式的格式化, #包含左右對齊, 設定字段寬度以及精度,添加符號(正負號)或者左填充數字0等. #字符串方法 有些很實用,比方split和join,有些則用得很少,比方istitle或capitalize. #本章的新函數 #string.capwords(s[, sep]) 使用split函數切割字符串s(以sep為分隔符),使用capitalize函數將切割得到的各單詞首字母大寫,而且使用join函數以sep為分隔符 #將各單詞連接起來 #string.maketrans(from,to) 創建用于轉換的轉換表 #接下來學什么 列表, 字符串和字典是Python中最重要的3種數據類型.

代碼清單3-1 字符串格式化演示樣例?

#e3-1 #使用給定的寬度打印格式化后的價格列表width = input('Plese enter width: ')price_width = 10 item_width = width - price_width;#減號(-1)用來左對齊數值 header_format = '%-*s%*s' format = '%-*s%*.2f'print '=' * widthprint header_format % (item_width, 'Item', price_width, 'Price')print '-' * widthprint format % (item_width, 'Apples', price_width, 0.4) print format % (item_width, 'Pears', price_width, 0.5) print format % (item_width, 'Cantaloupes', price_width, 1.92) print format % (item_width, 'Dried Apricots (16 oz.)', price_width, 8) print format % (item_width, 'Prunes (4 lbs.)', price_width, 12)print '=' * width#python e3-1.py #Plese enter width: 35 #=================================== #Item Price #----------------------------------- #Apples 0.40 #Pears 0.50 #Cantaloupes 1.92 #Dried Apricots (16 oz.) 8.00 #Prunes (4 lbs.) 12.00 #===================================

轉載于:https://www.cnblogs.com/brucemengbm/p/6978736.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的Python基础教程之第3章 使用字符串的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。