CodeSec QQȺ
| ¼
δDZ

Pythonȫջ֮·ϵַ֮

С | | С
[python python | С04 | ʱ 2017 | ] 0ղղ
ַ(str)

ַpythonͣıʾַУpythonַDzԸıģ޷ԭַ޸ģǿԽַһָƵµַУﵽͬ޸Ч

ַͿʹõŻ˫ֻʵ£

>>> string = 'ansheng' # typeDz鿴һ >>> type(string) <class 'str'>

˫

>>> string = "ansheng" # typeDz鿴һ >>> type(string) <class 'str'>

>>> string = """ansheng""" >>> type(string) <class 'str'>

ָ

>>> var=str("string") >>> var 'string' >>> type(var) <class 'str'> ַ

ÿķʵǺܶģѧϰĹиǹʱ򣬳õʵûж٣ûҪȥԵüô࣬ЩֻҪиӡokˣ˵ʱgoogleһ¡

ĸд

capitalize(self):

>>> name="ansheng" >>> name.capitalize() 'Ansheng'

ݾУwidthַܿȣfillcharַĬַΪո

center(self, width, fillchar=None):

# һַΪ"string"Ϊ"hello word" >>> string="hello word" # ַijȣlen(value_name) >>> len(string) 10 # ַܿΪ10ַΪ"*" >>> string.center(10,"*") 'hello word' # ַܲΪ11ôȥַ10ʣһλãλþͻᱻ*ռ >>> string.center(11,"*") '*hello word' # Ǵҿʼ >>> string.center(12,"*") '*hello word*'

ͳַijֵַĴ,ѡΪַĿʼλá

count(self, sub, start=None, end=None):

sub ַ; start ַʼλáĬΪһַ,һֵַΪ0; end ַнλáַеһַΪ 0ĬΪַһλ; >>> string="hello word" # Ĭ"l"dzֹε >>> string.count("l") 2 # ָӵλÿʼλã"l"ֹһ >>> string.count("l",3,6) 1

decode(self, encoding=None, errors=None):

# һΪ temp = "" # ѱַתΪUTF-8 temp_unicode = temp.decode("utf-8")

룬unicode

encode(self, encoding=None, errors=None):

# һΪ,ַΪUTF-8 temp = u"" # 룬ҪָҪתʲô temp_gbk = temp_unicode.encode("gbk")

жַǷָ׺βָ׺βTrue򷵻False

endswith(self, suffix, start=None, end=None):

suffix ׺һַҲѰҺ׺tuple start ʼƬ↑ʼ end ƬΪֹ >>> string="hello word" # жַǷ"d"β򷵻"True" >>> string.endswith("d") True # жַǷ"t"β򷵻"False" >>> string.endswith("t") False # ƶλãʵǴַλ17жϣ߸λ"d"򷵻True򷵻False >>> string.endswith("d",1,7) False

ַеtab('t')תΪոtab('t')ĬϵĿո8

expandtabs(self, tabsize=None):

tabsize ָתַе tab ('t')תΪոַ >>> string="hello word" # "string"ݵʱᷢмһ"\t"ʵһ`tab` >>> string 'hello\tword' # `tab`һո >>> string.expandtabs(1) 'hello word' # `tab`ʮո >>> string.expandtabs(10) 'hello word'

ַǷַstrָbeg(ʼ)end()ΧǷָΧڣַؿʼֵ򷵻-1

find(self, sub, start=None, end=None):

str ַָ beg ʼĬΪ0 end ĬΪַij >>> string="hello word" # `o`ڵǰַеλãҵһ`o`֮ͲټѰ >>> string.find("o") 4 # ӵλÿʼ`o`ڵλ >>> string.find("o",5) 7

ַʽ»ᵽ

format( args, *kwargs):

ַǷַ str ָ begʼ end ΧǷָΧڣ÷ python find()һֻstr stringлᱨһ쳣

index(self, sub, start=None, end=None):

str ַָ beg ʼĬΪ0 end ĬΪַij >>> string="hello word" # ַڵλ >>> string.index("o") 4 # һڵַôͻᱨ >>> string.index("a") Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: substring not found

ַǷĸɣstringһַַĸ򷵻True,򷵻False

isalnum(self):

>>> string="hes2323" # ֻĸͷ`True`򷵻`False` >>> string.isalnum() True # мпո񷵻صľFalse >>> string="hello word" >>> string.isalnum() False

ַǷֻĸɡ

isalpha(self):

# ȫĸͷ`True` >>> string="helloword" >>> string.isalpha() True # ͷFalse >>> string="hes2323" >>> string.isalpha() False

ַǷֻ

isdigit(self):

# 涼־ͷ`True`ͷ`False` >>> string="hes2323" >>> string.isdigit() False >>> string="2323" >>> string.isdigit() True

ַǷСдĸ

islower(self):

# ȫСдĸͷ`True`ͷ`False` >>> string="hesasdasd" >>> string.islower() True >>> string="HelloWord" >>> string.islower() False

ַǷֻɿո

isspace(self):

# ɿոɣôͷ`True`ͷ`False` >>> string=" " >>> string.isspace() True >>> string="a" >>> string.isspace() False

ַеĵƴдĸǷΪдĸΪСд

istitle(self):

# ĸǴдĸΪСдôͷ`True`᷵`False` >>> string="Hello Word" >>> string.istitle() True >>> string="Hello word" >>> string.istitle() False

ַеĸǷΪд

isupper(self):

# ֵеĸǴдͷ`True`ͷ`False` >>> string="hello word" >>> string.isupper() False >>> string="HELLO WORD" >>> string.isupper() True

еԪַָһµַ

join(self, iterable):

>>> string=("a","b","c") >>> '-'.join(string) 'a-b-c'

һԭַ,ʹÿոָȵַָijСԭַij򷵻ԭַ

ljust(self, width, fillchar=None):

width ַָ fillchar ַĬΪո >>> string="helo word" >>> len(string) 9 # ijȼȥַij,ʣµľͿʼ >>> string.ljust(15,'*') 'helo word******'

תַддַΪСд

lower(self):

# ѱĴдȫתСд >>> string="Hello WORD" >>> string.lower() 'hello word'

صַߵĿոַָ

lstrip(self, chars=None):

chars ָȡַ # ࿪ʼɾƥַ >>> string="hello word" >>> string.lstrip("hello ") 'word'

ָķַָзָַָķָ򷵻һ3ԪtupleһΪָߵӴڶΪָΪָұߵӴ

partition(self, sep):

str ָķָ # صһԪ >>> string="www.ansheng.me" >>> string.partition("ansheng") ('www.', 'ansheng', '.me')

ַе old(ַ)滻new(ַ)ָmax滻max

replace(self, old, new, count=None):

old 滻ַ new ַ滻oldַ count ѡַ, 滻count >>> string="www.ansheng.me" # Ѿַ`www.`ַ`https://` >>> string.replace("www.","https://") 'https://blog.ansheng.me' # ַ`w`ַ`a`ֻ滻`2` >>> string.replace("w","a",2) 'aaw.ansheng.me'

ַһγֵλãûƥ򷵻-1

rfind(self, sub, start=None, end=None):

str ҵַ beg ʼҵλãĬΪ0 end λãĬΪַij >>> string="hello word" # rfindʵǷ >>> string.rfind("o") 7 # ָҵķΧ >>> string.rfind("o",0,6) 4 ַstrֵַλãûƥַᱨ쳣ָѡ [beg:end] òҵ䡣

rindex(self, sub, start=None, end=None):

str ҵַ beg ʼҵλãĬΪ0 end λãĬΪַij >>> string="hello word" # >>> string.rindex("o") 7 # ûвҵͱ >>> string.rindex("a") Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: substring not found

һԭַҶ,ʹÿո width ַָijСַij򷵻ԭַ

rjust(self, width, fillchar=None):

width ַַָָܳ fillchar ַĬΪո >>> string="hello word" >>> len(string) 10 >>> string.rjust(10,"*") 'hello word' >>> string.rjust(12,"*") '**hello word'

ҵַָָͨƬ,numֵָָnumַ

rsplit(self, sep=None, maxsplit=None):

str ĬΪո num ָ >>> string="www.ansheng.me" >>> string.rsplit(".",1) ['www.ansheng', 'me'] >>> string.rsplit(".",2) ['www', 'ansheng', 'me']

ɾstringַĩβַָĬΪո.

rstrip(self, chars=None):

chars ָɾַ # βʼƥɾ >>> string="hello word" >>> string.rstrip("d") 'hello wor'

ַָָͨƬ,numֵָָnumַ

split(self, sep=None, maxsplit=None):

str ָĬΪո num ָ >>> string="www.ansheng.me" # ָһΣ`.`ָ >>> string.split(".",1) ['www', 'ansheng.me'] # ָжΣ`.`ָ >>> string.split(".",2) ['www', 'ansheng', 'me']

зָһΪԪصбnumָƬnum.

splitlines(self, keepends=False):

num ָеĴ # һлеı`\n`Ի >>> string="www\nansheng\nme" # >>> print(string) www ansheng me # еתһб >>> string.splitlines(1) ['www\n', 'ansheng\n', 'me']

ַǷַָͷ򷵻 True򷵻 False beg end ֵָָΧڼ顣

startswith(self, prefix, start=None, end=None):

str ַ strbeg ѡַʼλ strend ѡַĽλ >>> string="www.ansheng.me" >>> string.startswith("www") True >>> string.startswith("www",3) False

ƳַͷβַָĬΪո

strip(self, chars=None):

chars Ƴַͷβַָ >>> string=" www.ansheng.me " >>> string ' www.ansheng.me ' # ɾո >>> string.strip() 'www.ansheng.me' >>> string="_www.ansheng.me_" # ָҪߵ"_"ɾ >>> string.strip("_") 'www.ansheng.me'

ڶַĴСдĸתдСдСдд

swapcase(self):

>>> string="hello WORD" >>> string.swapcase() 'HELLO word'

"⻯"ַ,˵еʶԴдʼĸΪСд

title(self):

>>> string="hello word" >>> string.title() 'Hello Word'

ݲtableı( 256 ַ)תַַ, Ҫ˵ַŵ del С

translate(self, table, deletechars=None):

table ͨmaketransת deletechars ַҪ˵ַб

ַеСдĸתΪдĸ

upper(self):

>>> string="hello word" >>> string.upper() 'HELLO WORD'

ָȵַԭַҶ룬ǰ0

zfill(self, width):

width ַָijȡԭַҶ룬ǰ0 >>> string="hello word" >>> string.zfill(10) 'hello word' >>> string.zfill(20) '0000000000hello word' ȥֵ˿ո >>> var=" ansheng " >>> var ' ansheng ' >>> var.strip() 'ansheng' strͺbytesת

UTF-8ʱһֽڣһֽǰλ

3.5.xʵ

£

#!/usr/bin/env python # _*_ coding:utf-8 _*_ var = "" for n in var: print(n) print("================") var2 = "zhongwen" for n in var2: print(n)

ִн

C:\Python35\python.exe F:/Python_code/sublime/Day03/str.py ================ z h o n g w e n 2.7.xʵ

£

#!/usr/bin/env python # _*_ coding:utf-8 _*_ var = "" for n in var: print(n) print("================") var2 = "zhongwen" for n in var2: print(n)

ִн

C:\Python27\python.exe F:/Python_code/sublime/Day03/str.py ================ z h o n g w e n

ͨʵ֪ Python3.5.x ĻӢĵʱǰһַһַģ Python2.7.x Ͳˣ Python2.7.x ǰֽģԿĵʱģһΣΪUTF-8һǵֽڵģַ

Python3.5.xǼȿ֣Ҳ԰ֽڵģbytesbytesԽַתΪֽ

var="" for n in var: print(n) bytes_list = bytes(n, encoding='utf-8') # ʮ print(bytes_list) for x in bytes_list: # ʮ,bin(x) print(x,bin(x))

Ľ

# ַ # ʮ b'\xe4\xb8\xad' # 228=ʮƣ0b11100100= 228 0b11100100 184 0b10111000 173 0b10101101 b'\xe6\x96\x87' 230 0b11100110 150 0b10010110 135 0b10000111

bʮƣxe4һʮƵֽ

֪ʶ

ָijֵбеһλ

һб鿴б linux ֵӦбеλ

>>> list_os = ["windows","Linux","Mac","Unix"] >>> list_os.index("Linux") 1 >>> list_os[1] 'Linux' ʹ \ ת

PythonijЩַת壬ԴʵһЩԵַЧ

# õҲתҲ`\n``\t`ˣ`\n`еģ`\t`һ`tab` >>> string="My \n Name \t is" >>> print(string) My Name is ʹ + ƴ

ʹ + Žַַƴ

>>> a="my " >>> b="name " >>> c="is " >>> d="ansheng" >>> a+b+c+d 'my name is ansheng' Ƭ

ƬһţһԿѡ֣ðŷָעʹõʮơסǿѡģðDZģƬеĵһʾƬʼλãڶʾƬʾƬָһPythonʹ׿ʼûָڶPythonֹͣβע⣬صдӿʼλÿʼ պڽλ֮ǰʼλǰƬеģλñųƬ⡣

>>> os="Linux" >>> os 'Linux' >>> os[0:2] 'Li' >>> os[0:4:2] 'Ln'

ʵ

Ƭ ˵ [:] ȡӿͷβַ [start:] startβַ [:end] ӿͷȡend - 1 [start:end] startȡend - 1 [startsetp] startȡend-1ÿsetpַȡһ

ƬͬʱַбԪ

ͨڲijһֵַ

ƬͨڲijһΧڵֵַ

ʵ

# һббԪ >>> var=["Linux","Win","Unix"] # ͨȡһֵ >>> var[0] 'Linux' # ͨƬȡ˶ֵ >>> var[0:2] ['Linux', 'Win'] >>> var[1:3] ['Win', 'Unix']

ԭ

Pythonȫջ֮·ϵ

Ŀpython:python̳ python߳ webʦ ʦ

ҳ12
תע
ı⣺Pythonȫջ֮·ϵַ֮
վӣhttp://www.codesec.net/view/522958.html


1.CodeSecTeamתص,ý,ĿڴݸϢ,վͬ۵ʵԸ
2.תص½ԭ߹۵,뱾վ޹ءԭԼгֺδվ֤ʵ,վԸԼȫ߲ݡֵʵԡԡʱԣκα֤
3.籾վתظ漰Ȩ,߼ʱϵվ,ǻἰʱ
¼ӵղ¡עߵȨ...
| python | (0) | Ķ(73)