0
点赞
收藏
分享

微信扫一扫

python 字符串

天使魔鬼 2022-02-18 阅读 73


索引

s = 'hello'
print(s)

# 'h'
s[0]

#'ello'
s[1:]

#'o'
s[-1]

# 'ell'
s[1:4]

str='1234567890'

# 13579 步长截取为2
str[::2]





#0001230000
"123".center(10,"0")

#1230000000
"123".ljust(10,"0")

#0000000123
"123".rjust(10,"0")


strip

rstrip

lstrip

count

find

rfind

index

rindex

replace


str="aBcD"
#ABCD
print(str.upper())
#abcd
print(str.lower())
#AbCd 大小写互换
print(str.swapcase())
#Abcd 首字母大写
print(str.title())

#Abcd只返回字符串首字母大写
print(str.capitalize())

#a b
print("a\tb".expandtabs(5))


#数字字母 true
print("hello123".isalnum())
# false
print("hello123_".isalnum())

#完全字母组成false
print("hello123".isalpha())

#完全字母组成 true
print("hello".isalpha())

#完全数字组成 false
print("hello".isdigit())

#完全数字组成 true
print("123".isdigit())

#以什么结尾
print("hello world".endswith("d"))
print("hello world".endswith("ld"))


str = "a1234567890"
print(len(str))


#00042
print("42".zfill(5))

#-0042
print("-42".zfill(5))

#fbcfbcfbc
a = "abcabcabc"
print(a.replace("a", "f"))

a = b"abcbabc"
b = a.replace(b"a", b"f")
#b'fbcbfbc'
print(b)


#ABCDEF
print('abcdEf'.upper())
#True
print('ABC'.isupper())

#true
print(b'Py' in b'Python')


#index和find区别
#index找不到会报错 find找不到返回-1
#3
print("abcdef".find("d"))
# -1
print("abcdef".find("g"))

#3
print("abcdef".index("d"))
# 会报错
print("abcdef".index("g"))



#C:\some
#ame
print('C:\some\name')


# note the r before the quote
#C:\some\name
print(r'C:\some\name')




举报

相关推荐

0 条评论