例题引导:现有字符串“abcdefghi”,请使用切片分别提取出ceg和gec
解题关键:切片方法的应用。切片是字符串中很重要的一部分,其操作方法和元组、字典等都类似,大家一定要掌握。
参考答案:
words = "abcdefghi"
# 正向取
new_words1 = words[-7:-1:2]
# 反向取
new_words2 = words[-3:-8:-2]
print(new_words1)
print(new_words2)
学习引导:
字符串作为一种常见的数据类型,在日常中我们面临各式各样的字符串处理问题,那么,这就要求我们必须掌握一些常用的字符串处理函数。
下面我们就字符串的基本需求:查找、修改、删除、判断这四方面对字符串处理库进行简单的介绍。
常用于查找到函数有:find()、index()、count()
检测字符串是否包含特定字符,如果包含,则返回开始的索引;否则,返回-1。
str='Welcome to DataScience'
print(str.find('Da'))
#11
print(str.find('wc'))
#-1
检测字符串是否包含指定字符,如果包含,则返回开始的索引值;否则,提示错误。
print(str.index('come'))
#3
print(str.index('Date'))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-12-85801a960777> in <module>
----> 1 print(str.index('Date'))
ValueError: substring not found
返回str1在string中指定索引范围内[start, end)出现的次数。
print(str.count('e'))
#4
print(str.count('Da'))
#1
常见的用于字符串修改函数有replace()、split()、join()、capitalize()、title()、upper() 、loewr()、ljust()、rjust()、center()
把字符串中的 old(旧字符串)替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。
str='Welcome to Data Science'
print(str.replace("Data", "data"))
#Welcome to data Science
print(str.replace("e", "E", 3))
#WElcomE to Data SciEnce
str='Welcome to Data Science'
print(str.split(' ')) #用空格作为分节符
#['Welcome', 'to', 'Data', 'Science']
print(str.split('e',3))
#['W', 'lcom', ' to Data Sci', 'nce']
str='welcome to Data Science'
print(str.capitalize())
#Welcome to data science
seq='Welcome to Data Science'
str='-'
print(str.join(seq))
#W-e-l-c-o-m-e- -t-o- -D-a-t-a- -S-c-i-e-n-c-e
str='welcome to data science'
print(str.title())
#Welcome To Data Science
str='welcome,to data-science.' #加标点符号
print(str.title())
#Welcome,To Data-Science.
str='WELCOME TO DATA SCIENCE'
print(str.lower())
#welcome to data science
str='welcome to data science'
print(str.upper())
#WELCOME TO DATA SCIENCE
str='Welcome to data science'
print('str的原长度为%d'%(len(str)))
print('str处理后的长度为%d'%(len(str.ljust(30))))
print(str.ljust(30))
'''
str的原长度为23
str处理后的长度为30
Welcome to data science
'''
str='Welcome to data science'
print('str的原长度为%d'%(len(str)))
print('str处理后的长度为%d'%(len(str.rjust(30))))
print(str.rjust(30))
#str的原长度为23
#str处理后的长度为30
# Welcome to data science
str='Welcome to Data Science'
print(str.center(30))
print('str的原长度为%d'%(len(str)))
print('str处理后的长度为%d'%(len(str.center(30))))
# Welcome to Data Science
#str的原长度为23
#str处理后的长度为30
常用的字符串删除方法有:lstrip()、rstripe()、stripe()
str=' Welcome to Data Science'
print(str)
print(str.lstrip())
# Welcome to Data Science
#Welcome to Data Science
str='Welcome to Data Science '
print(str)
print(str.rstrip())
#Welcome to Data Science
#Welcome to Data Science
str=' Welcome to Data Science '
print(str)
print(str.strip())
# Welcome to Data Science
#Welcome to Data Science
常见的用于判断字符串的函数有:startwith()、endwith()、isalpha()、isdigit()、isalnum() 、isspace()
str='Welcome to Data Science'
print(str.startswith('W'))
print(str.startswith('w'))
#True
#False
str='Welcome to Data Science'
print(str.endswith('e'))
print(str.endswith('c'))
#True
#False
str='Welcome to Data Science'
print(str.isalpha()) #包含空格
#False
str_='asd'
print(str_.isalpha())
#True
str='Welcome to Data Science'
print(str.isdigit())
#False
str_='12345'
print(str_.isdigit())
#True
str='Welcome to Data Science'
print(str.isalnum())
#False
str_='123data'
print(str_.isalnum())
#True
str='Welcome to Data Science'
print(str.isspace())
#False
1.现有字符串“abcdefghi”,请使用切片分别提取出ceg和gec
2.回文数是指从左向右读和从右向左读都是一样的数,输入一个整数,判断该整数是否为回文数。
3.控制台输入任意字符串,将其去掉重复的字符后,按照字符ASCII码顺序从小到大排序后输出。例如:输入asdopwwsa16yy7aq,输出167adopqswy
4.将以下字符串按结果要求分割输出
#原字符串:
'better9late9than9never'
#分割后输出列表嵌套的字符串:
['better', 'late', 'than', 'never']