pythonベース--文字列
3539 ワード
# , , ,
# , ,
# python ,
# :
# ,
#
name = "wangcong"
print(name[1:3]) #
print(len(name)) #
# in or not in
print('a' in name) #
#
print('*' + '****') #*****
print('*' * 5) #*****
name = '*wangcong*'
print(name.strip("*")) #
print(name.rstrip("*"))#
print(name.lstrip("*")) #
name = ' wangcong '
print(name.strip()) # \t
name = 'WANGcong'
print(name.lower()) # ,
print(name.upper()) # ,
print(name) # name
name = 'wangcong'
print(name.startswith('wang')) # wang ,
print(name.endswith('cong')) # cong ,
print(name) # name
# format
people1 = "{} {} {}".format('wangcong',18,'male')
people2 = "{0} {1} {2}".format('wangcomg',18,'male')
people3 = "{name} {age} {sex}".format(sex='male',name = 'wangcong',age = 18)
print(people1,people2,people3)
print(name) # name
name = 'wang cong'
print(name.split()) # ,
print(name.split('o'))
# split
demo = 'a/b/c/d/e'
print(demo.split('/',1)) # ['a', 'b/c/d/e']
print(demo.split('/',2)) # ['a', 'b', 'c/d/e']
# rsplit
print(demo.rsplit('/',1)) # ['a/b/c/d', 'e']
print(name) # name
# join
name = ' '
print(name.join(['wang','cong'])) #
# join +
name = '*'
print(name.join(['w','a','n','g'])) # w*a*n*g
print(name + 'wang' + 'cong') # *wangcong
print(name) # name
# replace
name = 'wang *'
print(name.replace('*','cong')) # wang cong
#
name = 'wang *****'
print(name.replace('*','cong')) # wang congcongcongcongcong
print(name) # name
# find,rfind,index,rindex,count
str1 = 'hello world'
print(str1.find('l')) # 'l'
print(str1.find('b')) # -1
print(str1.find('l',3,5)) #
# rfind:
# index,rindex find,rfind
# count :
print(str1.count('l',1,4)) # ,
#
# \( ): ;\\:
#
: ;\t : ;\': ;\":
#
# %c: ASCII
print("%c"%89) # Y
print("%c"%'Y') #Y
# %s:
print("%s" %"wang cong") #wang cong
# %d
number = 87
print("%d" % number) # 87
# %u
# %o
print("%o" % number) # 1X27:
# %x ( )
number = 15
print("%x" % number) # f
# %X ( )
print("%X" % number)# F