字符串格式化

字符串格式化使用字符串格式化操作符%来实现:格式化字符串 % 值(字符串或者数字或者多个值的元组,字典)

>>> format = "hello, %s. %s are a good gril"
>>> values = ('zyj' ,'you')
>>> print(format % values)
hello, zyj. you are a good gril
>>>

若字符串中包含%,则需要%%进行处理

>>> format1 = 'percent:%.2f%%'
>>> values = 10.123
>>> print(format1 % values)
percent:10.12%
>>>   

可选参数:
参数1:转换标志:- 表示左对齐,默认为右对齐; + 表示在转换之前加上正负号; ''空白字符表示正数之前保留空格;0表示转换值若位数不够用0填充
参数2:最小字段宽度:转换后的字符串至少应该具有该值指定的宽度。如果是*,则宽度会从元组中提取
参数3:.后跟精度值:若为实数,精度值表示出现在小数点后的位数,若为字符串,那么该数字表示最大字段宽度。若是*精度将从元组中读出

>>> print('%x' % 16)
10
>>> print('%o' % 8)
10
>>> print('%s' % 'hello')
hello
>>> print('%r' % '42L')
'42L'
>>> from math import pi
>>> print('%10f' % pi)
3.141593
>>> print('%10.2f' % pi)
3.14
>>> print('%.2f' % pi)
3.14
>>> print('%10.5s' % 'hello,world')
hello
>>> format = '%*.*s'
>>> values = 'hello,world'
>>> print(format % (10,5,values))
hello
>>> print('%010.2f' % pi)
0000003.14
>>> print('%-10.2f' % pi)
3.14
>>> print(('% 5d' % 10) + '\n' +('% 5d' % -10))
10
-10
>>> print(('%+5d' % 10) + '\n' +('%+5d' % -10))
+10
-10
>>>

举例:在屏幕上打印九九乘法表:

print("九九乘法表:")
for x in range(1, 10):
for y in range(1, x + 1):
format1 = "%2d%s%-d%s%2d"
tuple1 = (x, ' * ', y, ' = ', x * y)
print(format1 % tuple1, end='')
print(" " * 4, end='')
print("\n") >>>
九九乘法表:
1 * 1 = 1 2 * 1 = 2 2 * 2 = 4 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36 7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49 8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64 9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81 >>>

字符串方法

find :在较长的字符串中查找子串。返回子串所在位置的最左端索引。如果没有找到返回-1
此方法可以接收可选的起始点和结束点参数;注意:包含第一个索引,但不包含第二个索引

>>> subject = "hello,world"
>>> print(subject.find('world',2))
6
>>>

join:用来连接序列中的元素,需要被连接的序列元素都必须是字符串。

>>> dirs ='','usr','bin','env'
>>> print('/'.join(dirs))
/usr/bin/env
>>> print('\\'.join(dirs))
\usr\bin\env
>>> ipAddr = ['12','1','1','1']
>>> netMask = ['255','255','255','0']
>>> print('.'.join(ipAddr)+' '+'.'.join(netMask))
12.1.1.1 255.255.255.0
>>>

split:将字符串分割成序列,方法中可以不提供分隔符。程序会把所有的空格作为分隔符

>>> print("I love you".split())
['I', 'love', 'you']
>>> print(list("I love you"))
['I', ' ', 'l', 'o', 'v', 'e', ' ', 'y', 'o', 'u']
>>> print(tuple("I love you"))
('I', ' ', 'l', 'o', 'v', 'e', ' ', 'y', 'o', 'u')
>>> print('1+2-3+5'.split('+'))
['1', '2-3', '5']
>>>

lower:返回字符串的小写字母,在‘不区分大小写’的时候,进行存储和查找。

>>> print(string1.lower())
hello,world
>>> print('hello' in string1.lower())
True
>>>

title:字符串中的所有字母的首字母大写。

>>> title1 = 'jave script'
>>> print(title1.title())
Jave Script
>>>

string模块的capwords函数

>>> import string
>>> print(string.capwords("jave script"))
Jave Script
>>>

replace:返回某字符串的所有匹配项均被替换之后得到的字符串

>>> string2 = 'this is a test'
>>> print(string2.title().replace('is','at'))
That Is A Test
>>> print(string2)
this is a test
>>>

strip:返回去除字符串两侧空格,可以增加参数以去掉特定的字符

>>> string3 = ' show memory -more- '
>>> print(string3.strip())
show memory -more-
>>> psws = ['a','b']
>>> psw = 'a '
>>> print(psw in psws)
False
>>> print(psw.strip() in psws)
True
>>> string4 ='####!!!!*****happy birthday to you*****####!!!!'
>>> print(string4.title().strip('#!'))
*****Happy Birthday To You*****
>>> print(string4.title().rstrip('!#'))
####!!!!*****Happy Birthday To You*****
>>> print(string4.title().lstrip('!#'))
*****Happy Birthday To You*****####!!!!
>>> string5 ='####!!!!###!!!####!!!!'
>>> print(string5.strip('#!')) >>>

translate:替换字符串中的某些部分,只能处理单个字符;如替换因平台而异的特殊字符。

python字符串的更多相关文章

  1. 关于python字符串连接的操作

    python字符串连接的N种方式 注:本文转自http://www.cnblogs.com/dream397/p/3925436.html 这是一篇不错的文章 故转 python中有很多字符串连接方式 ...

  2. StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing the strings?

    StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing t ...

  3. Python 字符串

    Python访问字符串中的值 Python不支持单字符类型,单字符也在Python也是作为一个字符串使用. Python访问子字符串,可以使用方括号来截取字符串,如下实例: #!/usr/bin/py ...

  4. python字符串方法的简单使用

    学习python字符串方法的使用,对书中列举的每种方法都做一个试用,将结果记录,方便以后查询. (1) s.capitalize() ;功能:返回字符串的的副本,并将首字母大写.使用如下: >& ...

  5. python字符串基础知识

    1.python字符串可以用"aaa",'aaa',"""aaa""这三种方式来表示 2.python中的转义字符串为" ...

  6. Python 字符串格式化

    Python 字符串格式化 Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存 一 ...

  7. Python 字符串操作

    Python 字符串操作(string替换.删除.截取.复制.连接.比较.查找.包含.大小写转换.分割等) 去空格及特殊符号 s.strip() .lstrip() .rstrip(',') 复制字符 ...

  8. 【C++实现python字符串函数库】strip、lstrip、rstrip方法

    [C++实现python字符串函数库]strip.lstrip.rstrip方法 这三个方法用于删除字符串首尾处指定的字符,默认删除空白符(包括'\n', '\r', '\t', ' '). s.st ...

  9. 【C++实现python字符串函数库】二:字符串匹配函数startswith与endswith

    [C++实现python字符串函数库]字符串匹配函数startswith与endswith 这两个函数用于匹配字符串的开头或末尾,判断是否包含另一个字符串,它们返回bool值.startswith() ...

  10. 【C++实现python字符串函数库】一:分割函数:split、rsplit

    [C++实现python字符串函数库]split()与rsplit()方法 前言 本系列文章将介绍python提供的字符串函数,并尝试使用C++来实现这些函数.这些C++函数在这里做单独的分析,最后我 ...

随机推荐

  1. memcached的key,value,过期时间的限制

    1.   key值最大长度? memcached的key的最大长度是250个字符,是memcached服务端的限制. 如果您使用的客户端支持"key的前缀"或类似特性,那么key( ...

  2. CSS百分比定义高度的冷知识

    当我们给块级元素设置响应式高度的时候,例如给div设置height=50%,往往没能看到效果. 原因是百分比的大小是相对其父级元素宽高的大小,如最外层元素设置的百分比是对应屏幕而言的. 需要了解的是对 ...

  3. Codeforces #261 D

    Codeforces #261 D D. Pashmak and Parmida's problem time limit per test 3 seconds memory limit per te ...

  4. Codeforces#262_1002

    Codeforces#262_1002 B. Little Dima and Equation time limit per test 1 second memory limit per test 2 ...

  5. 微信服务号模板消息接口新增"设置行业"和"添加模板"及细节优化

    微信服务号模板消息可以向用户发送重要的服务通知,如信用卡刷卡通知,商品购买成功通知等.昨日,微信团队发布公告称模板消息新增“设置行业”和“添加模板”接口及细节优化,详细变动如下 模板消息[业务通知]自 ...

  6. PHP安装模式cgi、fastcgi、php_mod比较

    先了解一下普通cgi的工作流程: web server收到用户请求,并把请求提交给cgi程序,cgi程序根据请求提交的参数作相应处理,然后输出标准的html语句返回给web server,web se ...

  7. PHP获取指定月份的第一天开始和最后一天结束的时间戳函数

    <?php /** * 获取指定月份的第一天开始和最后一天结束的时间戳 * * @param int $y 年份 $m 月份 * @return array(本月开始时间,本月结束时间) */ ...

  8. 【Android学习】《Android开发视频教程》第二季笔记(未完待续)

    视频地址: http://study.163.com/course/courseMain.htm?courseId=207001 课时22  Activity生命周期(一) 1.如何在一个应用中添加新 ...

  9. window共享linux下的文件 samba

    1.在Ubuntu上安装samba服务 sudo apt-get install samba 2.修改配置文件vim /etc/samba/smb.conf [xubu] (共享名) guest ac ...

  10. Extjs 制作柱状图

    在JSP页面制作柱状图,可以根据数据的变化动态实时的变化 主要是使用EXTJS自带的插件达到效果 Ext.require('Ext.chart.*'); Ext.require([ 'Ext.Wind ...