Python之str操作

1. str.format():使用“{}”占位符格式化字符串(占位符中的索引号形式和键值对形式可以混合使用)。

 1 >>> string = ‘python{}, django{}, tornado{}‘.format(2.7, ‘web‘, ‘tornado‘)  # 有多少个{}占位符就有多少个值与其对应,按照顺序“填”进字符串中
 2 >>> string
 3 ‘python2.7, djangoweb, tornadotornado‘
 4 >>> string = ‘python{}, django{}, tornado{}‘.format(2.7, ‘web‘)
 5 Traceback (most recent call last):
 6   File "<pyshell#6>", line 1, in <module>
 7     string = ‘python{}, django{}, tornado{}‘.format(2.7, ‘web‘)
 8 IndexError: tuple index out of range
 9 >>> string = ‘python{0}, django{2}, tornado{1}‘.format(2.7, ‘web‘, ‘tornado‘)  # 也可以指定“填”进去的值(从0开始,后面的值不一定都要用上,但是要保证指定的位置是有值的)
10 >>> string
11 ‘python2.7, djangotornado, tornadoweb‘
12 >>> string = ‘python{py}, django{dja}, tornado{tor}‘.format(tor=‘tornado‘, dja=‘web‘, py=2.7)  # 可以使用键值对的形式赋值
13 >>> string
14 ‘python2.7, djangoweb, tornadotornado‘
15 >>> 

2. 使用“%”进行字符串格式化。

格式化符号表
%c 转为单字符
%r 转为用repr()表达的字符串
%s 转为用str()表达的字符串
%d或%i 转为有符号的十进制整数
%u 转为无符号的十进制整数
%o 转为无符号的八进制整数
%x 转为无符号的十六进制整数,十六进制字母用小写表示
%X 转为无符号的十六进制整数, 十六进制字母用大写表示
%e 转为科学计数法表达的浮点数,其中的e用小写表示
%E 转为科学计数法表达的浮点数,其中的E用大写表示
%f或#F 转为浮点数
%g 由Python根据数字的大小自动判断转换为%e或%f
%G 由Python根据数字的大小自动判断转换为%E或%F
%% 输出“%”
辅助格式化符号表
* 定义宽度或小数点的精度
- 左对齐
+ 对正数输出正值符号“+”
<sp> 数字的大小不足m.n的要求时,用空格补位
# 在八进制数前显示0,在十六进制数前显示0x或0X
0 数字的大小不足m.n的要求时,用0补位
m.n m是显示的最小总宽度,n是小数点后的位数(如果可用)
时间: 2024-08-12 05:20:26

Python之str操作的相关文章

python MySQLdb 常用操作

我采用的是MySQLdb操作的MYSQL数据库.先来一个简单的例子吧: import MySQLdb try:     conn=MySQLdb.connect(host='localhost',user='root',passwd='root',db='test',port=3306)     cur=conn.cursor()     cur.execute('select * from user')     cur.close()     conn.close() except MySQL

使用python对redis操作

写在前面 首先声明,这是为了学习python对redis操作而写的一个小demo,包括了这几天网站找到的一些资料,综合总结出来一些东西,最后附上我写的一个用python操作redis的一个demo: 模块安装 python提供了一个模块redis-py来使我们很方便的操作redis数据库,安装该模块也很简单,直接使用pip安装就行,命令如下: pip install redis 安装完之后,使用import调用一下就能知道是否安装成功,在python界面下输入import redis,如果不报错

Python 列表(list)操作

创建列表 sample_list = ['a',1,('a','b')] Python 列表操作 sample_list = ['a','b',0,1,3] 得到列表中的某一个值 value_start = sample_list[0] end_value = sample_list[-1] 删除列表的第一个值 del sample_list[0] 在列表中插入一个值 sample_list[0:0] = ['sample value'] 得到列表的长度 list_length = len(sa

由 &#39;&#39; in &#39;abc&#39; return True 引发的思考----Python 成员测试操作

最近遇到判断字典中是否存在空字符串'',这个很好判断,直接用:'' in ['a','b','c'],就可以直接判断出来:但是当我对字符串使用 "in" 方法进行判断的时候,发现:'' in 'abc' 仍然会返回True,对于这个问题,之前一直没有注意到过其中的原理,现在去进行探索总结一下: 首先,查看官方文档:https://docs.python.org/2/reference/expressions.html#not-in 文档在5.9.2中:Membership test o

python的str,unicode对象的encode和decode方法

python的str,unicode对象的encode和decode方法 python中的str对象其实就是"8-bit string" ,字节字符串,本质上类似java中的byte[]. 而python中的unicode对象应该才是等同于java中的String对象,或本质上是java的char[]. 对于 s="你好" u=u"你好" s="你好" u=u"你好" 1. s.decode方法和u.enc

python自动化--模块操作之re、MySQL、Excel

一.python自有模块正则 1 import re 2 3 # re.match只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回None 4 print(re.match("www","wwwwccc").group()) #在起始位置匹配 5 print(re.match("www","wcccwww")) #不在起始位置匹配,返回None 6 7 # re.search扫描整个字符串并返回第一个成

python——TypeError: &#39;str&#39; does not support the buffer interface

import socket import sys port=51423 host="localhost" data=b"x"*10485760 #在字符串前加 b 是字符串变为bytes类. sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM) sock.connect((host,port)) byteswritten=0 while byteswritten<len(data): startpos =

python【二】python的字符串操作

python的字符串操作很灵活:先来看一个例子: str='helloworld' 首先我们输出这个字符串: print  str这是一种表示方法,还有很多表示的方法: 比如: print  str[0:10] 我们也能得到 helloworld print str[1:3] 我们得到:el,截取字符串的el两个字符: 截取字符串的前提条件: 是这样的我们如果要截取某段字符,首先要查找到这段字符的起始位置,然后才能正确截取:在python中提供了字符串查找的方法,这个跟c语言里面的字符查找很相似

关于python的字符串操作

字符串的判断操作: str = "fahaf asdkfja\t \r \n fjdhal 3453" print(str.isspace()) # 如果str中只包含空格,则返回True print(str.isalnum()) # 如果str至少有一个字符并且所有字符都是数字或者字母则返回True print(str.isalpha()) # 如果str至少有一个字符并且所有字符都是字母则返回True print(str.isdecimal()) # 如果string只包含数字则返