python基础语法之字符串
1 字符串中*的使用
*可以使字符串重复n次
print('hello world ' * 2) # hello world hello world
2 索引获取字符串的字符元素
print('hello world'[2:]) # llo world
3 in成员符
print('el' in 'hello') # True
4 字符串格式化
name = 'Bob'
msg = 'my name is %s' % name
print(msg) # my name is Bob
5 字符串拼接
采用+(建议尽量不要采用此方法,效率低,时间复杂度为平方级)
name = 'Bob'
age = ''
msg = name + ' is ' + age + ' years old!'
print(msg) # Bob is 20 years old!
采用join方法
字符串是join前面的那一字符串为拼接间隔
name = 'Bob'
age = ''
msg = name + ' is ' + age + ' years old!'
print(msg) # Bob is 20 years old!
msg_join = ' '.join([name, 'is', age, 'years old!'])
print(msg_join) # Bob is 20 years old!
6 字符串常用的内置方法
6.1 count() 可以计算参数中的字符串在原字符串中出现的数量
string = 'hello kitty'
count = string.count('l')
print(count) #
6.2 center()
string = 'title'
msg = string.center(50, '-')
print(msg) # ----------------------title-----------------------
6.3 startswith()
可以判断待处理的字符串是不是自己想要的字符串
string = 'title'
print(string.startswith('t')) # True
6.4 find()
查找参数中的字符串并返回索引,若找不到就返回-1
st = 'hello world'
index = st.find('w')
print(index) #
print(st.find('a')) # -1
6.5 index()
和find()差不多,只是在找不到的时候报错
st = 'hello world'
index = st.index('w')
print(index) #
print(st.index('a')) # ValueError: substring not found
6.6 lower()
将字符串中的大写字母变成小写字母
st = 'Hello World'
st_lower = st.lower()
print(st_lower) # hello world
6.7 upper()
将字符串中的小写字母变成大写字母
st = 'Hello World'
st_upper = st.upper()
print(st_upper) # HELLO WORLD
6.8 strip()
将待处理的字符串的空格,换行,制表符去掉
st = ' hello world\n'
st_strip = st.strip()
print(st_strip) # helloworld
print(len(st_strip)) #
6.9 replace()
st = 'hello world'
st_replace = st.replace('world', 'Bob')
print(st_replace) # hello Bob
6.10 split()
将字符串按照某字符进行分割,返回一个字符串元组
st = 'my old boy'
st_split = st.split(' ')
print(st_split) # ['my', 'old', 'boy']
python基础语法之字符串的更多相关文章
- 【python基础语法】字符串常用方法 、列表(第3天课堂笔记)
""" 字符串的方法 join 字符串拼接,将列表转换为字符串 find 查找元素位置 count 查找元素个数 replace 替换字符 split 字符串分割,将字符 ...
- Python基础语法day_02——字符串规则
day_02 使用方法修改字符串的大小写 将字符串首字母变成大写 >>> name = "ada lovelace" >>> print(nam ...
- python基础语法_字符串编码
Python常用字符编码 http://www.cnblogs.com/schut/p/8406897.html Python常见字符编码间的转换 在字符串写入文件时,有时会因编码问题导致无法 ...
- python之最强王者(2)——python基础语法
背景介绍:由于本人一直做java开发,也是从txt开始写hello,world,使用javac命令编译,一直到使用myeclipse,其中的道理和辛酸都懂(请容许我擦干眼角的泪水),所以对于pytho ...
- Python 基础语法(四)
Python 基础语法(四) --------------------------------------------接 Python 基础语法(三)------------------------- ...
- Python 基础语法(二)
Python 基础语法(二) --------------------------------------------接 Python 基础语法(一) ------------------------ ...
- Python 基础语法
Python 基础语法 Python语言与Perl,C和Java等语言有许多相似之处.但是,也存在一些差异. 第一个Python程序 E:\Python>python Python 3.3.5 ...
- 吾八哥学Python(四):了解Python基础语法(下)
咱们接着上篇的语法学习,继续了解学习Python基础语法. 数据类型大体上把Python中的数据类型分为如下几类:Number(数字),String(字符串).List(列表).Dictionary( ...
- python学习第四讲,python基础语法之判断语句,循环语句
目录 python学习第四讲,python基础语法之判断语句,选择语句,循环语句 一丶判断语句 if 1.if 语法 2. if else 语法 3. if 进阶 if elif else 二丶运算符 ...
随机推荐
- 调试python 程序的几种方法总结
程序能一次写完并正常运行的概率很小,基本不超过1%.总会有各种各样的bug需要修正.有的bug很简单,看看错误信息就知道,有的bug很复杂,我们需要知道出错时,哪些变量的值是正确的,哪些变量的值是错误 ...
- 【CF160E】Buses and People
题目大意:给定 N 个三元组 (a,b,c),现有 M 个询问,每个询问给定一个三元组 (a',b',c'),求满足 a<a', b'<b, c'<c 的最小 c 对应的元组编号. ...
- Atcoder Regular Contest 066 F genocide【JZOJ5451】
题目 分析 \(s[i]\)表示a前缀和. 设\(f[i]\)表示做完了1~i的友谊颗粒的最优值(不一定选i),那么转移方程为 \[f[i]=max\{f[i-1],max\{f[j]-s[i]+s[ ...
- 写在centos7 最小化安装之后
1.最小化安装之后首先解决联网问题(https://lintut.com/how-to-setup-network-after-rhelcentos-7-minimal-installation/) ...
- Mysql中几种sql的常见用法
如何使用非默认的排序.例如使用213之类的排序 可以使用如下方法 SELECT DISTINCT pg.part_grp_id, pg.part_grp_name, pg.equip_category ...
- C# Winform程序之间如何传递和接收参数 Process
Program: static class Program { /// <summary> /// 应用程序的主入口点. /// </summa ...
- luoguP1772 [ZJOI2006]物流运输 x
P1772 [ZJOI2006]物流运输 题目描述 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线 ...
- CentOS版本禁用Ctrl+Alt+Del重启功能
1 禁用Ctrl+Alt+Del重启功能(不重启系统的前提条件) 1.1 CentOS 6 ##查看/etc/inittab确认Ctrl+Alt+Del相关配置文件 cat /etc/initta ...
- Unity3D_(游戏)2D简单游戏制作过程:捕获高空掉落保龄球
游戏介绍:通过鼠标的左右移动,可以控制帽子的移动,当帽子接到下落的保龄球时,会出现火花效果.没有接到保龄球时,保龄球落到草地上,过10S后会自动消失. 实现效果: 素材+Unity3D源代码:传送 ...
- 分布式-信息方式-ActiveMQ示例
实战 代码如下: 信息生产者 package test.mq.helloword; import javax.jms.Connection; import javax.jms.ConnectionFa ...