字符串
Posted 一蓑烟雨
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了字符串相关的知识,希望对你有一定的参考价值。
>>> a=‘abcdefg‘ >>> a ‘abcdefg‘ >>> a[::-2] ‘geca‘ >>> a[::] ‘abcdefg‘ >>> a[::-1] ‘gfedcba‘ >>> a = ‘abcdef‘ >>> a ‘abcdef‘ >>> a[::-1] ‘fedcba‘ >>> a ‘abcdef‘ >>> a[4:3] ‘‘ >>> a[4:3:-1] ‘e‘
>>> mystr = ‘hello world wanglinjie linjie w_linjie‘ >>> mystr.find("linjie") 16 >>> mystr.find("linjie",0,10) -1 >>> mystr.count("linjie") 3
>>> mystr.replace("linjie","LinJie") ‘hello world wangLinJie LinJie w_LinJie‘
>>> mystr.replace("linjie","LinJie",1) ‘hello world wangLinJie linjie w_linjie‘
>>> mystr.split(" ") [‘hello‘, ‘world‘, ‘wanglinjie‘, ‘linjie‘, ‘w_linjie‘]
>>> mystr.split(" ",2) [‘hello‘, ‘world‘, ‘wanglinjie linjie w_linjie‘]
>>> mystr.split(" ",3) [‘hello‘, ‘world‘, ‘wanglinjie‘, ‘linjie w_linjie‘]
>>> mystr.capitalize() ‘Hello world wanglinjie linjie w_linjie‘
>>> mystr.title() ‘Hello World Wanglinjie Linjie W_Linjie‘
>>> mystr.startswith("hello") True >>> mystr.startswith("Hello") False >>> mystr.endswith("jie") True >>> mystr.endswith("Jie") False >>>
>>> Mystr=ystr = ‘Hello World Wanglinjie Linjie W_Linjie‘ >>> Mystr.lower() ‘hello world wanglinjie linjie w_linjie‘ >>>
>>> Mystr=ystr = ‘Hello World Wanglinjie Linjie W_Linjie‘
>>> Mystr.lower() ‘hello world wanglinjie linjie w_linjie‘
>>> Mystr.upper() ‘HELLO WORLD WANGLINJIE LINJIE W_LINJIE‘
>>> Mystr.ljust(10) ‘Hello World Wanglinjie Linjie W_Linjie‘
>>> my = ‘wang‘ >>> my.ljust(10) ‘wang ‘>>> mystr.rjust(10) ‘hello world wanglinjie linjie w_linjie‘ >>> my.rjust(10) ‘ wang‘ >>> mystr.center(10) ‘hello world wanglinjie linjie w_linjie‘ >>> my.center(10) ‘ wang ‘ >>> my.lstrip() ‘wang‘ >>> a = ‘ linjie ‘ >>> a.lstrip() ‘linjie ‘ >>> a.rstrip() ‘ linjie‘ >>> a.strip() ‘linjie‘ >>>
>>> mystr ‘hello world wanglinjie linjie w_linjie‘ >>> mystr.rfind("linjie") 32 >>> mystr.partition("linjie") (‘hello world wang‘, ‘linjie‘, ‘ linjie w_linjie‘) >>> mystr.rpartition("linjie") (‘hello world wanglinjie linjie w_‘, ‘linjie‘, ‘‘)
>>> mystr = ‘abc‘ >>> mystr.isalpha() True >>> mystr.isdigit() False >>> mystr1 = ‘123‘ >>> mystr1.isalpha() False >>> mystr1.isdigit() True >>> mystr2 = ‘abc123‘ >>> mystr2.isalnum() True >>> mystr1.isalnum() True >>> mystr.isalnum() True >>> mystr3 = ‘ ‘ >>> mystr3.isalnum() False >>> mystr3.isspace() True
>>> mystr = ‘ ‘ >>> li = ["my","name","is","linjie"]>>> mystr.join(li) ‘my name is linjie‘ >>> a = ‘_‘ >>> a.join(li) ‘my_name_is_linjie‘ >>>
以上是关于字符串的主要内容,如果未能解决你的问题,请参考以下文章