当前位置:网站首页>字符串方法练习
字符串方法练习
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.边栏推荐
- 如何用项目甘特图,做好项目汇报
- When you complain about the roll, others have quietly begun to prepare for the golden three silver four
- Dialogue with opensea co creation Alex: we still only touch the tip of the iceberg of NFT capability | chain catcher
- How to become a senior digital IC Design Engineer (4-3)
- Is the ETF fund reliable and safe
- 如何成为一名高级数字 IC 设计工程师(4-5)脚本篇:Shell 脚本实现的文件比较操作
- Kubernetes: container resource requirements and constraints (constraints)
- I have summarized some experiences from the whole process of R & D platform splitting
- Configurable FFT system design based on risc-v SOC (1) Introduction
- How to encrypt the device
猜你喜欢

SQL splits strings into tables
![[redis] hash type](/img/8b/2585908318f5fe88b455323d3f392e.png)
[redis] hash type

如何成为一名高级数字 IC 设计工程师(4-2)脚本篇:Verilog HDL 代码实现的文件读写操作

Live broadcast preview | can SQL also play industrial machine learning? Mlops meetup V3 takes you to the bottom!

Business system anti-virus

Exclusive analysis | real situation of software test about resume and interview

How to use project Gantt chart to make project report

OculusRiftS与Unity.UI的交互(1)-总览
![[机缘参悟-33]:眼见不一定为实,大多数时候“眼见为虚”](/img/60/703a5bc3038d28bcf812415032f240.jpg)
[机缘参悟-33]:眼见不一定为实,大多数时候“眼见为虚”

To apply for a test engineer after years, the resume with high scores should be written like this
随机推荐
Scala 基础 (三):运算符和流程控制
Which is the best billing method for okcc call center
Utiliser kubernets Resource Lock pour compléter son application ha
Is the ETF fund reliable and safe
数字 IC 设计、FPGA 设计秋招笔试题目、答案、解析(2)2021 华为海思(上)
如何成为一名高级数字 IC 设计工程师(6-5)数字 IC 验证篇:覆盖率收集
e. Difference between target and e.currenttarget
基于 FPGA 的 RISC CPU 设计(4)关于项目的 36 个问题及其答案
11 go Foundation: Interface
【Redis】SortedSet类型
Business system anti-virus
Kubernetes: container resource requirements and constraints (constraints)
SAP ui5 beginner tutorial 24 - how to use OData data model
[redis] set type
利用kubernetes資源鎖完成自己的HA應用
e.target与e.currentTarget的区别
Which brokerage is safer and more convenient to open an account for compass mobile stock?
东方财富股票开户是会有什么风险吗?东方财富开户安全吗
为什么建议大家尽可能选择自研公司
Digital IC design, FPGA design written examination questions, answers and analysis of autumn move (1) 2022 Ziguang zhanrui (Part 1)