Python_day01——字符串

本文详细介绍了Python中字符串的各种操作方法,包括长度计算、大小写转换、子串查找、切片、数字与字母检查等,是Python编程学习者的重要参考资料。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://www.cnblogs.com/A-FM/p/5691468.html

def main():
    str1 = 'hello, world!'
    # 通过len函数计算字符串的长度
    print(len(str1))  # 13
    # 获得字符串首字母大写的拷贝
    print(str1.capitalize())  # Hello, world!
    # 获得字符串变大写后的拷贝
    print(str1.upper())  # HELLO, WORLD!
    # 从字符串中查找子串所在位置
    print(str1.find('or'))  # 8
    print(str1.find('shit'))  # -1
    # 与find类似但找不到子串时会引发异常
    # print(str1.index('or'))
    # print(str1.index('shit'))
    # 检查字符串是否以指定的字符串开头
    print(str1.startswith('He'))  # False
    print(str1.startswith('hel'))  # True
    # 检查字符串是否以指定的字符串结尾
    print(str1.endswith('!'))  # True
    # 将字符串以指定的宽度居中并在两侧填充指定的字符
    print(str1.center(50, '*'))
    # 将字符串以指定的宽度靠右放置左侧填充指定的字符
    print(str1.rjust(50, ' '))
    str2 = 'abc123456'
    # 从字符串中取出指定位置的字符(下标运算)
    print(str2[2])  # c
    # 字符串切片(从指定的开始索引到指定的结束索引)
    print(str2[2:5])  # c12
    print(str2[2:])  # c123456
    print(str2[2::2])  # c246
    print(str2[::2])  # ac246
    print(str2[::-1])  # 654321cba
    print(str2[-3:-1])  # 45
    # 检查字符串是否由数字构成
    print(str2.isdigit())  # False
    # 检查字符串是否以字母构成
    print(str2.isalpha())  # False
    # 检查字符串是否以数字和字母构成
    print(str2.isalnum())  # True
    str3 = '  jackfrued@126.com '
    print(str3)
    # 获得字符串修剪左右两侧空格的拷贝
    print(str3.strip())


if __name__ == '__main__':
    main()

 

转载于:https://www.cnblogs.com/long5683/p/10837767.html

### 关于 Python 中字典和字符串的练习题与教程 #### 字典基础操作练习 在 Python 中,字典是一种非常灵活的数据结构,允许存储键值对。下面是一些针对字典的基础练习: 1. 创建一个新的字典 `my_dict` 并初始化它包含三个键值对:"name": "Alice", "age": 25, 和 "city": "Beijing"[^1]。 ```python my_dict = {"name": "Alice", "age": 25, "city": "Beijing"} print(my_dict) ``` 2. 访问并打印出字典中的 `"name"` 键对应的值[^2]。 ```python print(my_dict["name"]) ``` 3. 更新字典中 `"age"` 的值为 26。 ```python my_dict["age"] = 26 print(my_dict) ``` 4. 添加新的键值对到字典里,比如 `"country": "China"`。 ```python my_dict["country"] = "China" print(my_dict) ``` 5. 删除字典里的某个项,例如移除 `"city"` 这个条目。 ```python del my_dict["city"] print(my_dict) ``` #### 字符串基本操作练习 对于字符串而言,在 Python 中可以执行多种多样的操作来满足不同的需求。这里给出几个简单的例子作为入门级练习: 1. 定义一个变量 `greeting` 赋予其值 `"Hello, world!"`,然后将其转换成全部大写字母形式显示出来。 ```python greeting = "Hello, world!" print(greeting.upper()) ``` 2. 使用字符串的方法统计单词数量,假设有一个句子 stored_sentence = 'Python is an easy to learn powerful programming language.' ,计算其中有多少个单词。 ```python stored_sentence = 'Python is an easy to learn powerful programming language.' word_count = len(stored_sentence.split()) print(f"The sentence contains {word_count} words.") ``` 3. 将两个字符串连接起来形成一个新的字符串。 ```python first_part = "Good morning," second_part = " have a nice day." message = first_part + second_part print(message) ``` 这些只是初步接触 Python 字典和字符串的方式之一,更多高级特性和技巧可以在实际项目开发过程中逐步学习掌握。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值