当前位置:网站首页>字符串方法练习
字符串方法练习
2022-06-29 02:12:00 【长街395】
描述
字符串的常用方法
| 方法名 | 描述 |
|---|---|
| str.upper() | 转换字符串str中所有字母为大写 |
| str.lower() | 转换字符串str中所有字母为小写 |
| str.strip() | 用于移除字符串头尾指定的字符(缺省时去掉空字符,包括\t、\n、\r、\x0b、\x0c等) |
| str.split() | 通过指定分隔符对字符串进行切片,将一个字符串分裂成多个字符串列表,缺省时根据空格切分,可指定逗号或制表符等。 |
| str.join(seq) | 以字符串str作为分隔符,将序列 seq 中所有的元素合并为一个新的字符串 |
| str.find() | 搜索指定字符串 |
| str.replace(old, new[, count]) | 把字符串str中的 old 替换成 new,如果 count 指定,则替换不超过 count 次,否则有多个old子串时全部替换为new。 |
| for <var> in <string> | 字符串迭代,如果var在string中存在,则进行循环 |
请参考上表,根据注释的要求完成模板程序。
s = input() # 输入一个英文字符串
print(s.upper()) # 将字符串中所有英文字符转为大写输出
print(s.lower()) # 将字符串中所有英文字符转为小写输出
t = s.split() # 将输入的字符串根据空格切分为列表,命名为t,每个单词为一个元素
print(t) # 输出切分的列表
print(' '.join(t)) # 将列表t中的元素用空格连接后输出
print('-'.join(t)) # 将列表t中的元素用连字符“-”连接后输出
print('/'.join(t)) # 将列表t中的元素用'/'连接后输出
print(s.find('the')) # 搜索字符串s中是否存在字符串'the',如存在返回其位置序号
r = s.replace('the','a') # 将字符串s中所有的'the'替换为'a',替换后的字符串重新命名为r
print(r) # 输出替换后的字符串r
r = s.replace('the','a',1) # 将字符串s中的第一个'the'替换为'a',替换后的字符串重新命名为r
print(r) # 输出替换后的字符串r
# Between the cup and the lip a morsel may slip.
# Where there is life, there is hope.
# That man is the richest whose pleasure are the cheapest.边栏推荐
- Understand flex layout in an article
- 同花顺究竟是如何开户?在线开户是安全么?
- SAP ui5 beginner tutorial Part 23 - sorting sort and grouping Group trial version of list control
- 在按钮禁用时消除hover效果
- QT basics tutorial: data types and containers
- [redis] get to know redis for the first time
- I'd like to ask you, where can I open an account in Zhongshan? Is it safe to open an account online?
- Scala Foundation (3): Operators and Process Control
- [understanding of opportunity -33]: seeing is not necessarily true. Most of the time, "seeing is false"
- Zhongyi technology resumed the review status of the gem IPO, and xuxiaofei no longer acted as a practicing lawyer
猜你喜欢
![[high concurrency, high performance and high availability of massive data MySQL practice-10] - Implementation of mvcc in InnoDB](/img/dc/a30ccd9943e668aef8c874980a4975.jpg)
[high concurrency, high performance and high availability of massive data MySQL practice-10] - Implementation of mvcc in InnoDB

我把整个研发中台拆分过程的一些心得总结
![[redis] set type](/img/97/0a83016e89316849c01072044f2124.png)
[redis] set type

Large scale visual relationship understanding

Differences between web testing and app testing

Flex application: realize two rows and five columns of data, self-adaptive

Project R & D, what are the free brain mapping tools that are easy to use

How to prevent virus
Scala 基礎 (三):運算符和流程控制
Scala Foundation (3): Operators and Process Control
随机推荐
How to encrypt anti copy program
【Redis】Hash类型
Koa 快速入門
想请教一下,我在中山,到哪里开户比较好?在线开户是安全么?
Fundamentals of scala (3): operators and process control
MySQL详解 --- 聚合与分组
Junior final exam
[understanding of opportunity -33]: seeing is not necessarily true. Most of the time, "seeing is false"
To apply for a test engineer after years, the resume with high scores should be written like this
How to become a senior digital IC Design Engineer (6-7) digital IC Verification: debug skills
Eliminate the hover effect when the button is disabled
指南针手机股票开户哪个券商更安全更方便?
基于 FPGA 的 RISC CPU 设计(4)关于项目的 36 个问题及其答案
How to use project Gantt chart to make project report
SAP ui5 beginner tutorial 22 - development and use of filter
Is there any risk in opening an account for Dongfang fortune stock? Is it safe for Dongfang fortune to open an account
[从零开始学习FPGA编程-49]:视野篇 - 芯片是如何被设计出来的?
Crawler exercise (IV) -- IP address problem
如何成为一名高级数字 IC 设计工程师(6-6)数字 IC 验证篇:系统级仿真
Blog publishing test 2