str2='you need Python'
时间: 2023-05-29 21:03:59 浏览: 52
str2.lower() # 'you need python'
str2.upper() # 'YOU NEED PYTHON'
str2.capitalize() # 'You need python'
str2.title() # 'You Need Python'
str2.replace('Python', 'Java') # 'you need Java'
相关问题
(1)创建名为str1的字符串对象”Life is short, you need Python.”; (2)利用字符串索引或切片分别读取(输出)字符串str1中的’Python’子串、’Life’子串 (3)分别使用index或find方法查找子串’short’的索引位置、查找子串’you’在[16,19]索引范围内的首次出现的位置(索引); (4)通过字符串切片获取str1的子串”you need Python”,将其赋给新的字符串str2; (5) 利用lower、upper、切片等方法分别输出字符串str2的大写形式、小写形式和”You need PYTHON” 形式; (6) 通过split方法对字符串str1分别按照空字符串和逗号进行分割,并求相应的分割后元素个数; (7)通过range函数创建1-20之间的所有偶数对应的数字字符串构成的列表num1,并通过print输出所有数字字符串元素; (8)将num1中所有字符元素通过“-”连接成一个新字符串str3,并计算字符串str3中”-”出现的次数。
1. str1 = "Life is short, you need Python."
2. print(str1[12:18]) # 输出 Python
print(str1[:4]) # 输出 Life
3. print(str1.index("short")) # 输出 8
print(str1.find("you", 16, 19)) # 输出 16
4. str2 = str1[12:]
5. print(str2.upper()) # 输出 YOU NEED PYTHON
print(str2.lower()) # 输出 you need python
print(str2.capitalize()) # 输出 You need python
6. print(len(str1.split())) # 输出 7
print(len(str1.split(","))) # 输出 2
7. num1 = [str(x) for x in range(2, 21, 2)]
print(num1) # 输出 ['2', '4', '6', '8', '10', '12', '14', '16', '18', '20']
8. str3 = "-".join(num1)
print(str3.count("-")) # 输出 9
利用Python IDLE完成以下语句的编程: (1)创建名为str1的字符串对象”Life is short. you need Pvthon.": (2)利用字符串索引或切片分别读取(输出)字符串str1中的’Python'子串、’Life’子串 (3)分别使用index或find方法查找子串‘short’的索引位置、查找子串·you'在[16,19索引范围内的首次出现的位置(索引); (4)通过字符串切片获取strl的子串”you need Python”,将其赋给新的字符串str2; (5)利用lower、upper、切片等方法分别输出字符串str2的大写形式、小写形式和”You need PYTHON"形式; (6)通过split方法对字符串str1分别按照空字符串和逗号进行分割,并求相应的分割后元素个数; (7)通过range函数创建1-20之间的所有偶数对应的数字字符串构成的列表numl,并通过print输出所有数字字符串元素; (8)将numl中所有字符元素通过“-”连接成一个新字符串str3,并计算字符串str3中”-”出现的次数。
(1)
str1 = "Life is short. you need Python."
(2)
print(str1[16:22]) # Python
print(str1[:4]) # Life
(3)
print(str1.index("short")) # 8
print(str1.find("you", 16, 19)) # 16
(4)
str2 = str1[12:26]
(5)
print(str2.upper()) # YOU NEED PYTHON
print(str2.lower()) # you need python
print(str2[:4] + str2[4:8].upper() + str2[8:]) # You need PYTHON
(6)
split_by_space = str1.split(" ")
split_by_comma = str1.split(",")
print(len(split_by_space)) # 6
print(len(split_by_comma)) # 1
(7)
numl = [str(num) for num in range(2, 21, 2)]
print(numl) # ['2', '4', '6', '8', '10', '12', '14', '16', '18', '20']
(8)
str3 = "-".join(numl)
print(str3.count("-")) # 9
相关推荐
![mobi](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)