国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Python標(biāo)準(zhǔn)庫筆記之string 

http://blog.mcxiaoke.com/2015/07/15/python-module-string-notes/

2015

String

1
2
3
4
5
6
7
8

# -*- coding: UTF-8 -*-
# Created by mcxiaoke on 15/7/4 17:01.
__author__ = 'mcxiaoke'

import datetime
import string
from string import Template

一些常量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
'''
whitespace = ' \t\n\r\v\f'
lowercase = 'abcdefghijklmnopqrstuvwxyz'
uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
letters = lowercase + uppercase
ascii_lowercase = lowercase
ascii_uppercase = uppercase
ascii_letters = ascii_lowercase + ascii_uppercase
digits = '0123456789'
hexdigits = digits + 'abcdef' + 'ABCDEF'
octdigits = '01234567'
punctuation = """!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
printable = digits + letters + punctuation + whitespace
'''

字符串格式化

1
2
3
4
5
6
7
8
9
10
11
# 字符串格式化格式為 "hello, world {0} {1}"
# 默認(rèn)調(diào)用 __format()__方法,可以顯示使用str()或repr()
# "Harold's a clever {0!s}" # Calls str() on the argument first
# "Bring out the holy {name!r}" # Calls repr() on the argument first
print "hello, world {0} {1} !".format('first', 'second')

# 字符串格式 %s
print "Welcome to %s" % "China"
# 命名參數(shù)
print 'I love %(cat)s and %(dog)s.' % {'cat': 'BadCat', 'dog': 'BadDog'}
# out:I love BadCat and BadDog.

整數(shù)格式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
'''
'b' 二進(jìn)制整數(shù)
'c' Unicode字符
'd' 十進(jìn)制整數(shù)
'o' 八進(jìn)制整數(shù)
'x' 十六進(jìn)制,小寫
'X' 十六進(jìn)制,大寫
'n' 十進(jìn)制,帶分隔符
'''
num = 1234567890
print "十進(jìn)制:%d 八進(jìn)制:%o 十六進(jìn)制:0x%x 十六進(jìn)制:0X%X" % (num, num, num, num)
# out: 十進(jìn)制:1234567890 八進(jìn)制:11145401322 十六進(jìn)制:0x499602d2 十六進(jìn)制:0X499602D2
# 浮點數(shù)格式
fnum = 987123456.78901234567890123
print '%e' % fnum, '%E' % fnum, '%f' % fnum, '%F' % fnum, '%g' % fnum, '%G' % fnum
# out: 9.871235e+08 9.871235E+08 987123456.789012 987123456.789012 9.87123e+08 9.87123E+08

格式化例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
print '{0}, {1}, {2}'.format('a', 'b', 'c')  # a, b, c
print '{}, {}, {}'.format('a', 'b', 'c') # a, b, c
print '{2}, {1}, {0}'.format('a', 'b', 'c') # c, b, a
print '{2}, {1}, {0}'.format(*'abc') # 參數(shù)解包
print '{0}{1}{0}'.format('hello-', 'world-') # 參數(shù)可以重復(fù)使用
# out: hello-world-hello-

# 命名參數(shù)例子 # out: 經(jīng)緯度: (37.24N, -115.81W)
print '經(jīng)緯度: ({0}, {1})'.format('37.24N', '-115.81W')
print 'A 經(jīng)緯度: ({latitude}, {longitude})'.format(latitude='37.24N', longitude='-115.81W')
args = ('37.24N', '-115.81W')
print 'B 經(jīng)緯度: ({}, {})'.format(*args)
kwargs = {'latitude': '37.24N', 'longitude': '-115.81W'}
print 'C 經(jīng)緯度: ({latitude}, {longitude})'.format(**kwargs)

自定義對象例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Point(object):
def __init__(self, x, y):
self.x, self.y = x, y

def __str__(self):
return "Point({self.x},{self.y})".format(self=self)

def __repr__(self):
return "(x={self.x},y={self.y})".format(self=self)


pt = Point(15, 238)
print 'Point is {}'.format(pt) # Point is Point(15,238)
print 'Point(x={0.x}, y={0.y})'.format(pt) # Point(x=15, y=238)
two = ('hello', 'world')
print 'A={0[0]}, B={0[1]}'.format(two) # A=hello, B=world
# 使用 repr()和str()的示例
print "repr() shows quotes: {!r}; str() doesn't: {!s}".format('test1', 'test2')
print 'Point is {0!r} {0!s}'.format(pt) # Point is (x=15,y=238) Point(15,238)

對齊方式

1
2
3
4
# 星號用于加強顯示效果,默認(rèn)是空白
print '{:*<30}'.format('左對齊') # 左對齊*********************
print '{:*>30}'.format('右對齊') # *********************右對齊
print '{:*^30}'.format('居中對其') # *********居中對其*********

數(shù)字格式處理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 不帶前綴
print "int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}".format(2015)
# out: int: 2015; hex: 7df; oct: 3737; bin: 11111011111
# 帶前綴
print "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}".format(2015)
# int: 2015; hex: 0x7df; oct: 0o3737; bin: 0b11111011111
# 添加分隔符
print '{:,}'.format(1234567890) # 1,234,567,890
# 日期時間格式化
d = datetime.datetime.now()
print '{:%Y-%m-%d %H:%M:%S}'.format(d) # 2015-07-04 18:34:28

# 字符串模板
s = Template('$who likes to eat $what')
print s.substitute(who='john', what='fish') # john likes to eat fish

字符串函數(shù)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
s = 'this is some sample text.'
# 首字母大寫
print string.capitalize(s) # This is some sample text.
# 結(jié)果等于 s.capitalize()
# 查找字符串
print s.find('is') # 2
print s.find('hello') # -1
# 分割字符串
print s.split(' ') # ['this', 'is', 'some', 'sample', 'text.']
print s.split(' ', 1) # ['this', 'is some sample text.']
print s.rsplit(' ', 1) # ['this is some sample', 'text.']
# 合并字符串
print s.join(['A_', 'B']) # A_this is some sample text.B
print string.join(['there', 'are', 'joined', 'words'], ' ') # there are joined words
s2 = 'this\tis\tsome\tsample\ttext'
# 展開TAB
print s2.expandtabs(4) # this is some sample text
s3 = ' hello, python! '
# 檢查結(jié)尾
print s.endswith('hello') # out:False
# 去除前后空白
print s3.strip() # hello, python!
s4 = 'THIS is SOME text.'
# 交換大小寫
print s4.swapcase() # this IS some TEXT.
# 首字母大寫
print s4.capitalize() # This is some text.
# 標(biāo)題模式,單詞首字母大寫
print 'title:', s4.title() # This Is Some Text.
# 全部小寫
print s4.lower() # this is some text.
# 全部大寫
print s4.upper() # THIS IS SOME TEXT.
# 全是字母和數(shù)字
print '12345678abcdefg'.isalnum() # out:True
# 全是數(shù)字
print '12345'.isdigit() # out:True
# 全是字母
print '12345, hello'.isalpha() # out:False
# 刪除指定字符
print 'hello,world,I love cats'.translate(None, 'o') # out:hell,wrld,I lve cats
# 補齊字符串
print s4.ljust(30, '_') # THIS is SOME text.____________
print s4.rjust(30, '_') # ____________THIS is SOME text.
print s4.center(30, '_') # ______THIS is SOME text.______
print s4.zfill(30) # 000000000000THIS is SOME text.

替換字符串

1
2
3
4
5
6
7
8
9
s5 = 'color1 and color2 and color3 scheme files are tested.'
print s5.replace('color', 'dark')
# out: dark1 and dark2 and dark3 scheme files are tested.
print s5.replace('and', '')
# out: color1 color2 color3 scheme files are tested.
print s5.replace(' ', '_')
# out: color1_and_color2_and_color3_scheme_files_are_tested.
print s5.replace(' ', '-', 2)
# out: color1-and-color2 and color3 scheme files are tested.
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
這有 73 個例子,徹底掌握 f-string 用法!
可能是最全面的 Python 字符串拼接總結(jié)
Python最常用的基礎(chǔ)語句分享!
string標(biāo)簽
string.Format效率問題 - .NET技術(shù) / C#
pyhton 面向?qū)ο笾?小明左右手換牌
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服