部分及不熟
# 6.1.字符串字面量
spam='Hello,there!nHow are you?nI'am fine.'
print(spam)
# Hello,there!
# How are you?
# I'am fine.
##原始字符串;在字符串开始的引号之前加上r
spam=r'Hello,there!nHow are you?nI'am fine.'
print(spam) #Hello,there!nHow are you?nI'am fine.
# 6.1.3 控制付出包含在所有的字符串里边
print('' in 'hello') #True
# 6.2 将字符串放入其他字符串
#方式1:字符串连接
name = 'Kim'
age = '24'
print("My name is "+ name +",I'm "+ age +" years old!" )
#方式2:字符串插值,利用运算符% 当做标记
name='Kim'
age='24'
print("My name is %s,I'm %s years old!" % (name,age)) #引号和%没有逗号,
#方式3:f字符串
name = 'Kim'
age = '24'
print(f"My name is {name},I'm {age } years old!" )
#6.3.1 isupper()、islower()
spam='Hello,world!'
print(spam.islower())
print(spam.isupper())
print('abcd12345'.isupper()) #False
print('abcd12345'.islower()) #True
print('12345'.isupper()) #False
print('12345'.islower()) #False
print('Hello,world!'.upper()) #HELLO,WORLD!
print('Hello,world!'.upper().isupper()) #True
print('Hello,world!'.upper().lower()) #hello,world!
#6.3.2 isX()字符串方法
#isalpha()方法,如果字符串只包含字母,并且非空,返回True
print( 'hello'.isalpha() ) #True
print( 'hello123'.isalpha() ) #False
#isalnum()方法,如果字符串只包含字母环和数字,并且非空,返回True
print( 'hello'.isalnum() ) #True
print( 'hello123'.isalnum() ) #True
#isdecimal()方法,如果字符串只包含数字字符,并且非空,返回True
print( '123'.isdecimal() ) #True
#isspace()方法,如果字符串只包含空格、制表符环绕和换行符,并且非空,返回True
print( ' '.isspace() ) #True
#istitle()方法,(首字母大写)如果字符串仅包含以大写字母开头,后边都是小写字母的单词、数字或空格,返回True
print( 'This Is Title Case'.istitle() ) #True
print( 'This Is Title Case,123'.istitle() ) #True
print( 'This is Title Case'.istitle() ) #False
print( 'This IS Title Case'.istitle() ) #False
# while True:
# print('Enter your age:')
# age = input()
# if age.isdecimal():
# break
# print('Please enter a number for your age.')
#
# while True:
# print('Select a new password (letters and numbers only):')
# password = input()
# if password.isalnum():
# break
# print('Passwords can only have letters and numbers.')
#6.3.2 字符串方法 startswith() 和 endswith()
print( 'Hello,world'.startswith('Hello') ) #True
print( 'Hello,world'.endswith('Hello') ) #False
print( 'abcd123'.startswith('abcdef') ) #False
print( 'abcd123'.startswith('abc') ) #True
print( 'abcd123'.startswith('abcd') ) #True
print( 'abcd123'.startswith('abcd123') ) #True
print( 'abcd123'.endswith('12') ) #False
print( 'abcd123'.endswith('123') ) #True
print( 'abcd123'.endswith('d123') ) #True
print( 'abcd123'.endswith('abcd123') ) #True
#字符串方法join():将字符串列表。连接成一个字符串
print( ','.join( ['cat','dog','pig']) ) #cat,dog,pig
print( '-'.join( ['cat','dog','pig']) ) #cat-dog-pig
print( ' '.join( ['My','name','is','Kim','!']) ) #My name is Kim !
print( 'ABC'.join( ['My','name','is','Kim','!']) ) #MyABCnameABCisABCKimABC!
#字符串方法split():针对一个字符串的调用,返回一个字符串列表
print( 'My name is Kim !'.split() ) #['My', 'name', 'is', 'Kim', '!']
print( 'MyABCnameABCisABCKimABC!'.split('ABC') ) #['My', 'name', 'is', 'Kim', '!']
print( 'My name is Kim !'.split('m') ) #['My na', 'e is Ki', ' !']
##split()常用于按照换行符分隔多行字符串
spam='''Dear Gege,
maybe I have fall love with you,
I very miss you,
I donnot know if you also miss me.'''
print( spam.split('n') )
#['Dear Gege,', 'maybe I have fall love with you,', 'I very miss you,', 'I donnot know if you also miss me.']
# 6.3.5 使用partition()方法分隔字符串,形成一个元祖
print('Hello,world!'.partition('w')) #('Hello,', 'w', 'orld!')
print('Hello,world!'.partition('world')) #('Hello,', 'world', '!')
##如果有重复的字符,以第一个为准进行分隔
print('Hello,world!'.partition('o')) #('Hell', 'o', ',world!')
##如果找不到分隔字符串,则返回元组中的 第一个字符串就是整个字符串,其他2个字符串为 空
print('Hello,world!'.partition('XYZ')) #('Hello,world!', '', '')
print( 'abcd 123'.partition('XYZ') ) #('abcd 123', '', '')
##利用多重赋值将三个返回的字符串赋给3个变量
befor,sep,after ='Hello world!'.partition(' ')
print(befor) #Hello
print(sep) #空格
print(after) #world
#6.3.6 rjust()、ljust()
print( 'Hello'.rjust(10) )
print( 'Hello'.ljust(10) )
print( 'Hello'.rjust(10,'-') ) #-----Hello
print( 'Hello'.ljust(10,'=') ) #Hello=====
##center()
print( 'Hello'.center(10) ) # Hello
print( 'Hello'.center(10,'=') ) #==Hello===
def printPicnic(itemsDict, leftWidth, rightWidth):
print('PICNIC ITEMS'.center(leftWidth + rightWidth, '-'))
for k, v in itemsDict.items():
print( k.ljust(leftWidth, '.') + str(v).rjust(rightWidth) )
picnicItems = {'sandwiches': 4, 'apples': 12, 'cups': 4, 'cookies': 8000}
printPicnic(picnicItems, 12, 5)
printPicnic(picnicItems, 20, 6)
# ---PICNIC ITEMS--
# sandwiches.. 4
# apples...... 12
# cups........ 4
# cookies..... 8000
# -------PICNIC ITEMS-------
# sandwiches.......... 4
# apples.............. 12
# cups................ 4
# cookies............. 8000
#strip()方法:返回一个新的字符串,开头和结尾都没有空格
#也可带有一个可选的字符串参数,用于指定哪些字符应该删除
spam=' hello,Gege '
print( spam.strip() ) #hello,Gege
message='SpamSpamhelloSpamGegeSpamSpamSpam'
print( message.strip('Spam') ) #helloSpamGege
#lstrip()
spam=' hello,Gege '
print( spam.lstrip() ) #hello,Gege
#rstrip()
spam=' hello,Gege '
print( spam.rstrip() ) # hello,Gege
# 6.4 ord()和chr()函数的字符的数值
print( ord('A') ) #65
print( chr(65) ) # A
print( ord('A') < ord('C') )
print( ord('A') +1 )
print( chr( ord('A') +1 ) ) # B
# #用pyperclip模块赋值粘贴字符串
# import pyperclip
# pyperclip.copy('Hello,world!')
# pyperclip.paste()
# pyperclip.paste()