字符串格式化

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

>>> 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. jquery + header

    官网上搜索headers 基本用法(直接用下楼上的代码了) $.ajax({ //请求类型,这里为POST type: 'POST', //你要请求的api的URL url: url , //是否使用 ...

  2. Bootstrap学习------按钮

    Bootstrap为我们提供了按钮组的样式,博主写了几个简单的例子,以后也许用的到. 效果如下 代码如下 <!DOCTYPE html> <html> <head> ...

  3. postgresql:pgadmin函数调试工具安装过程

    通过安装第三方插件pldebugger,可实现在pgadmin客户端对函数设置断点.调试,具体过程如下: 1.下载pldebugger安装包:http://git.postgresql.org/git ...

  4. MRDS学习四——自动型机器车

    由自己的所在开始,探索自己周围的简单机器车,假设车子的行走路径如下: 我们要把L型路径写成一个Activity,然后由外部输入这个L的大小,最后这个Activity要能够在完成行走路径时吐出更大的L大 ...

  5. Alpha版本十天冲刺——Day 10

    站立式会议 最后一天,很高兴我们做出了跟预期差不多的版本,实现了基本功能,虽然还有一些bug,但是下一阶段我们会继续加油! 会议总结 队员 今天完成 遇到的问题 感想 鲍亮 功能细节更改 我的手机运行 ...

  6. 多文件上传 file-uploader.js

    插件暴露给用户可以设置的参数 插件构成 声明一个全局对象qq,在对象上封装几个方法,类似JQUERY的方法 qq.extend 合并对象属性,类似$.extend() qq.indexOf 获取元素索 ...

  7. SharePreferences的DB实现

    存储一些简单数据的时候,最快的实现是用SharePreferences,但SharePreferences的可靠性不高,在某些非官方ROM上,总是存取失败.后来想到用数据库来存取.产品中,存取的数据项 ...

  8. bootstrap 笔记

    导航条: div.navbar.navbar-default > ul.nav.navbar 无非两层: 父div: navbar {相对位置.最低高度50px.下部margin:20px.透明 ...

  9. 使用 CoordinatorLayout 实现复杂联动效果

    GitHub 地址已更新: unixzii / android-FancyBehaviorDemo CoordinatorLayout 是 Google 在 Design Support 包中提供的一 ...

  10. 【目录】python

    python 入门学习(一) 入门学习(二) 入门学习(三) 入门学习(四) 入门学习(五) 入门学习(六) 入门学习(七) 入门学习(八) 入门学习(九) 入门学习(十) Head First Py ...