哪个网站可以做销售记录仪,wordpress底部版权怎么修改,教人做网站的视频,广州网站建设联系新科海珠你需要知道的python字符串函数
format
字符串的format函数为非字符串对象嵌入字符串提供了一种非常强大的方法。在format方法中#xff0c;字符串使用{}来代替一系列字符串的参数并规定格式。下面通过几个例子来详细解释其用法
直接使用{}
apple_num 10
print(I have {…你需要知道的python字符串函数
format
字符串的format函数为非字符串对象嵌入字符串提供了一种非常强大的方法。在format方法中字符串使用{}来代替一系列字符串的参数并规定格式。下面通过几个例子来详细解释其用法
直接使用{}
apple_num 10
print(I have {} apples.format(apple_num))
在{}中使用位置参数1
nums [4, 5, 6]
msg Numbers: {0} {1} {0}.format(nums[0], nums[1])
print(msg)
# Numbers: 4 5 4
在{}中使用位置参数2
msg Numbers: {a} {c} {b}.format(a5, b6, c7)
print(msg)
# Numbers: 5 7 6
{}中的更多格式
_ [print({}x{}{:4}.format(y, x, x*y), end\n if xy else ) for x in range(1, 10) for y in range(1, x1)]
:4表示左对齐占用四格位置其结果为
1x11
1x22 2x24
1x33 2x36 3x39
1x44 2x48 3x412 4x416
1x55 2x510 3x515 4x520 5x525
1x66 2x612 3x618 4x624 5x630 6x636
1x77 2x714 3x721 4x728 5x735 6x742 7x749
1x88 2x816 3x824 4x832 5x840 6x848 7x856 8x864
1x99 2x918 3x927 4x936 5x945 6x954 7x963 8x972 9x981
join()
joins a list of strings with another string as a separator
print(, .join([spam, eggs, ham]))
# spam, eggs, ham
split
join的逆向
print(spam, eggs, ham.split(, ))
# [spam, eggs, ham]
replace()
replaces one substring in a string with another
print(Hello ME.replace(ME, world)
# Hello world
startwith, endwith
determine if there is a substring at the start and end of a string, respectively.
print(This is a sentence.startwith(This))
# True
print(This is a sentence.endwith(sentence))
# False
lower, upper
change the case of a string
print(hello world.upper())
# HELLO WORLD
print(HELLO WORLD.lower())
# hello world