4.Python字符串和列表:字符串输入、字符串输出、下标和切片、字符串常见函数、列表(list)、列表的循环遍历、列表的增删改查、列表的嵌套、列表的切片

1. Python 字符串

1.1 字符串输入

input() 函数用于从用户获取字符串输入。它总是返回一个字符串类型的值。

python 复制代码
# 从用户输入字符串
name = input("请输入你的名字:")
print(f"你好, {name}")
1.2 字符串输出

字符串的输出通常使用 print() 函数。可以直接打印字符串,也可以通过格式化输出。

python 复制代码
# 直接输出字符串
print("Hello, World!")

# 格式化输出
name = "Alice"
age = 25
print(f"名字是 {name}, 年龄是 {age}")
1.3 下标和切片
  • 下标(Index) :通过下标可以访问字符串中的单个字符,Python 中的字符串下标从 0 开始。负数下标表示从字符串的尾部访问。
python 复制代码
my_string = "Hello"
print(my_string[0])  # 输出 'H'
print(my_string[-1])  # 输出 'o'(从后向前)
  • 切片(Slicing) :通过切片可以获取字符串的子串。切片语法为 my_string[start:end],其中 start 为开始位置,end 为结束位置,但不包含 end
python 复制代码
my_string = "Hello, World!"
print(my_string[0:5])  # 输出 'Hello',从下标 0 到 4
print(my_string[7:12])  # 输出 'World'
print(my_string[:5])  # 输出 'Hello',从开头到下标 4
print(my_string[7:])  # 输出 'World!',从下标 7 到末尾
1.4 字符串常见函数
  • len():返回字符串的长度

    python 复制代码
    print(len("Hello"))  # 5
  • lower():将字符串转换为小写

    python 复制代码
    print("HELLO".lower())  # "hello"
  • upper():将字符串转换为大写

    python 复制代码
    print("hello".upper())  # "HELLO"
  • strip():去除字符串两端的空白字符

    python 复制代码
    print("  hello  ".strip())  # "hello"
  • replace():替换字符串中的指定部分

    python 复制代码
    print("Hello, World!".replace("World", "Python"))  # "Hello, Python!"
  • split():将字符串分割成多个子字符串,返回一个列表

    python 复制代码
    print("apple,banana,orange".split(","))  # ['apple', 'banana', 'orange']
  • join():将列表中的字符串连接为一个字符串

    python 复制代码
    print(", ".join(['apple', 'banana', 'orange']))  # 'apple, banana, orange'

2. Python 列表

2.1 列表(List)

列表是一个有序的集合,可以包含多个元素,且支持修改。列表的元素可以是任何类型的数据,包括数字、字符串、甚至其他列表。

python 复制代码
my_list = [1, 2, 3, 4, 5]
print(my_list)  # 输出 [1, 2, 3, 4, 5]
2.2 列表的循环遍历

你可以使用 for 循环来遍历列表中的每一个元素。

python 复制代码
my_list = [1, 2, 3, 4, 5]
for item in my_list:
    print(item)
2.3 列表的增删改查
  • 添加元素 :使用 append() 向列表末尾添加元素,使用 insert() 在指定位置插入元素。

    python 复制代码
    my_list = [1, 2, 3]
    my_list.append(4)  # 添加元素到末尾
    print(my_list)  # [1, 2, 3, 4]
    
    my_list.insert(1, 1.5)  # 在位置 1 插入元素 1.5
    print(my_list)  # [1, 1.5, 2, 3, 4]
  • 删除元素 :使用 remove() 删除指定值的元素,使用 pop() 删除指定位置的元素并返回其值。

    python 复制代码
    my_list = [1, 2, 3, 4]
    my_list.remove(3)  # 删除元素 3
    print(my_list)  # [1, 2, 4]
    
    popped_item = my_list.pop(1)  # 删除并返回位置 1 的元素
    print(popped_item)  # 2
    print(my_list)  # [1, 4]
  • 修改元素:直接通过下标来修改列表中的元素。

    python 复制代码
    my_list = [1, 2, 3, 4]
    my_list[1] = 10  # 修改下标 1 位置的元素
    print(my_list)  # [1, 10, 3, 4]
  • 查找元素 :使用 in 来判断一个元素是否在列表中,使用 index() 来获取元素的下标。

    python 复制代码
    my_list = [1, 2, 3, 4]
    print(3 in my_list)  # True
    print(my_list.index(4))  # 3
2.4 列表的嵌套

列表可以嵌套其他列表,形成多维结构。

python 复制代码
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(nested_list[0])  # 输出 [1, 2, 3]
print(nested_list[1][2])  # 输出 6
2.5 列表的切片

与字符串一样,列表也可以通过切片来访问一部分元素。

python 复制代码
my_list = [1, 2, 3, 4, 5]
print(my_list[1:4])  # 输出 [2, 3, 4]
print(my_list[:3])   # 输出 [1, 2, 3]
print(my_list[2:])   # 输出 [3, 4, 5]

总结

  • 字符串操作 :Python 中字符串是不可变的,可以使用下标和切片进行访问和截取,常用的字符串函数包括 lower(), upper(), strip(), replace() 等。
  • 列表操作 :列表是可变的,可以包含任意类型的数据,可以使用 append(), insert(), remove() 等方法进行增删改查,支持嵌套和切片操作。
相关推荐
pumpkin845142 小时前
Rust Mock 工具
开发语言·rust
SSH_55232 小时前
【大模型】情绪对话模型项目研发
人工智能·python·语言模型
love530love2 小时前
【笔记】在 MSYS2(MINGW64)中安装 python-maturin 的记录
运维·开发语言·人工智能·windows·笔记·python
阿卡蒂奥3 小时前
C# 结合PaddleOCRSharp搭建Http网络服务
开发语言·http·c#
泉飒5 小时前
lua注意事项
开发语言·笔记·lua
hao_wujing6 小时前
使用逆强化学习对网络攻击者的行为偏好进行建模
开发语言·网络·php
G皮T6 小时前
【Python Cookbook】文件与 IO(二)
python·i/o·io·文件·gzip·stringio·bytesio
还是鼠鼠6 小时前
单元测试-概述&入门
java·开发语言·后端·单元测试·log4j·maven
封奚泽优6 小时前
使用Python绘制节日祝福——以端午节和儿童节为例
人工智能·python·深度学习
干啥都是小小白6 小时前
话题通信之python实现
python·机器人·ros