利用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中”-”出现的次数。
时间: 2023-05-29 20:06:01 浏览: 78
(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
相关问题
idle python,Python IDLE:更改Python版本
如果你想在 Python IDLE 中使用不同的 Python 版本,可以按照以下步骤操作:
1. 确认你已经安装了要使用的 Python 版本。
2. 打开 Python IDLE。
3. 从菜单栏选择 "Options",然后选择 "Configure IDLE"。
4. 在弹出的窗口中,选择 "General" 选项卡。
5. 在 "Python Shell" 部分,找到 "Python Executable" 选项。
6. 点击 "Browse" 按钮,并选择要使用的 Python 版本的可执行文件。
7. 点击 "Apply" 和 "OK"。
现在,Python IDLE 将使用你选择的 Python 版本。
python idle怎么创建包
要在Python IDLE中创建包,可以按照以下步骤进行操作:
1. 打开Python IDLE,创建一个新的文件夹作为你的包。例如,你可以在桌面上创建一个名为“mypackage”的文件夹。
2. 在“mypackage”文件夹中,创建一个名为“__init__.py”的空文件。这个文件指示Python解释器该文件夹是一个包。
3. 在“mypackage”文件夹中,创建一个或多个模块文件。例如,你可以创建一个名为“module1.py”的文件。
4. 在“module1.py”中,可以定义函数、类等。例如,你可以在“module1.py”中定义一个名为“myfunction”的函数。
5. 最后,在Python IDLE中,可以使用以下语句来导入你的包和模块:
```python
import mypackage.module1
```
或者,可以使用以下语句来导入包中的函数:
```python
from mypackage.module1 import myfunction
```
这样,你就可以在Python IDLE中使用你的包和模块了。