本文共 3958 字,大约阅读时间需要 13 分钟。
# Python3>>> name1 = "Joe">>> name2 = "Mary">>> print(f"你好 {name1}, {name2} 在哪?")你好 Joe, Mary 在哪?
# Python3>>> myStr = str("THIS IS TEST")>>> myStr'THIS IS TEST'>>> foo = myStr.lower()>>> foo'this is test'
# Python3>>> aa = "hello world">>> aa * 3'hello worldhello worldhello world'
# Python3>>> "hello" + " world"'hello world'
# Python3>>> "hello" < "world"True>>> 0 if "hello" == "world" else (-1 if "hello" < "world" else 1)-1>>> "hello" == "world"False
# Ruby[13] pry(main)> "hello" < "world"=> true[14] pry(main)> "hello" <=> "world"=> -1[15] pry(main)> "hello" == "world"=> false
# Python3>>> import re>>> re.search("ll", "hello world")<_sre.SRE_Match object; span=(2, 4), match='ll'>>>> re.search("o.*o", "hello world")<_sre.SRE_Match object; span=(4, 8), match='o wo'>
# Python3>>> "hello world"[0:5]'hello'>>> "hello world"[0:-6]'hello'
# Python3>>> aa = "hello world">>> aa.capitalize()'Hello world'
和 lower() 比较像,casefold 支持很多不同种类的语言。比如说 β。str.lower() 只能显示出原形而 casefold 则能显示他的小写。
# Python3>>> "Hello woRld ß".casefold()'hello world ss'>>> "Hello woRld .ß".lower()'hello world ß'
# Python3>>> aa = "hello world">>> aa.center(20)' hello world '>>> aa.center(20, '*')'****hello world*****'
# Python3>>> "hello world".count('o')2
# Python3>>> aa = "hello world">>> aa.endswith("ld")True>>> aa.endswith("l")False
跟 find() 方法一样,只不过如果 substr 不在字符串中会报一个异常。
# Python3>>> aa = "hello world">>> aa.find('o')4>>> aa.index('o')4>>> aa.index('ol')Traceback (most recent call last): File "", line 1, in ValueError: substring not found>>> aa.find('ol')-1
# Python3>>> aa = "hello world">>> aa.isalnum()False>>> aa = "helloworld5">>> aa.isalnum()True
# Python3>>> aa = "helloworld">>> aa.isalpha()True>>> aa = "hello world">>> aa.isalpha()False
str.isdigit() 检测字符串是否只包含数字(即不接受其他一切非 [0-9] 元素)。
# Python3>>> aa = "hello world">>> aa.isdigit()False>>> aa = "12345">>> aa.isdigit()True
# Python3>>> "12345Ⅲ".isnumeric()True>>> "12345.67".isnumeric()False
# Python3>>> "hello world".islower()True>>> "Hello world".islower()False
# Python3>>> "hello world".isupper()False>>> "HELLO WORLD".isupper()True
# Python3>>> "hello world".isspace()False>>> " ".isspace()True
# Python3>>> "hello world".istitle()False>>> "Hello World".istitle()True
# Python3>>> "2345".isdecimal()True>>> "0aff".isdecimal()False
# Python3>>> aa = ['h', 'e', 'l', 'l', 'o']>>> "".join(aa)'hello'>>> "-".join(aa)'h-e-l-l-o'
# Python3>>> "hello world".ljust(20)'hello world '>>> "hello world".ljust(20, '-')'hello world---------'
# Python3>>> " hello world".lstrip()'hello world'
# Python3>>> "Hello World".lower()'hello world'
# Python3>>> "hello world".replace("world", "hello")'hello hello'
# Python3>>> "hello world".rjust(20)' hello world'>>> "hello world".rjust(20, '-')'---------hello world'
# Python3>>> "hello world ".rstrip()'hello world'
# Python3>>> " hello world ".strip()'hello world'
# Python3>>> "hello world".split()['hello', 'world']>>> "hello world".split('l')['he', '', 'o wor', 'd']>>> "hello world".split('l', 2)['he', '', 'o world']
# Python3>>> "hello world".startswith("hello")True
# Python3>>> "Hello World".swapcase()'hELLO wORLD'
# Python3>>> "hello world".title()'Hello World'
# Python3>>> "Hello World".upper()'HELLO WORLD'
# Python3>>> "10110".zfill(8)'00010110'
# Python3>>> len("hello world")11
# Python3>>> max("hello world")'w'
# Python3>>> min("hello world")' '
转载地址:http://ajjqi.baihongyu.com/