python笔记03:使用字符串
3.1 基本字符串操作:
3.2 字符串格式化:精简版
>>> format = "Hello,%s,%s enough for ya?"
>>> values = ('world','Hot')
>>> print format % values
Hello,world,Hot enough for ya?
>>> format = "Pi with three decimals: %.3f"
>>> from math import pi
>>> print format % pi
Pi with three decimals: 3.142
3.2.1 模版字符串
>>> from string import Template
>>> s = Template('$x,glorious $x!')
>>> s.substitute(x='slurm')
'slurm,glorious slurm!'
>>> s = Template("It's ${x}tastic")
>>> s.substitute(x='slurm')
"It's slurmtastic"
>>> s = Template("Make $$ selling $x!")
>>> s.substitute(x='slurm')
'Make $ selling slurm!'
>>> s = Template('A $thing must never $action.')
>>> d = {}
>>> d['thing'] = 'gentleman'
>>> d['action'] = 'show his socks'
>>> s.substitute(d)
'A gentleman must never show his socks.'
3.3 字符串格式化:完整版
>>> '%s plus %s equals %s' % (1,2,3)
'1 plus 2 equals 3'
>>> '%s plus % equals %s.' % 1,2,3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: not enough arguments for format string
3.3.1 基本的转换说明符
| 转换类型 | 含义 |
| d,i | 带符号的十进制整数 |
| o | 不带符号的八进制 |
| u | 不带符号的十进制 |
| x | 不带符号的十六进制(小写) |
| X | 不带符号的十六进制(大写) |
| e | 科学技术法表示的浮点数(小写) |
| E | 科学技术法表示的浮点数(大写) |
| f,F | 十进制浮点数 |
| g | 如果指数大于-4或者小于精度值则和e相同,其他情况与e相同 |
| G | 如果指数大于-4或者小于精度值则和e相同,其他情况与E相同 |
| C | 单字符(接收整数或者单字符字符串) |
| r | 字符串(使用repr转换任意python对象) |
| s | 字符串(使用str转换任意python对象) |
3.3.2 简单转换
>>> 'Price of eggs: %d' % 42
'Price of eggs: 42'
>>> 'Hexadecimal price of eggs: %x.' % 42
'Hexadecimal price of eggs: 2a.'
>>> from math import pi
>>> 'Pi: %f...' %pi
'Pi: 3.141593...'
>>> 'Pi: %f...' % pi
'Pi: 3.141593...'
>>> 'Very inexact estimate of pi: %i.' % pi
'Very inexact estimate of pi: 3.'
>>> 'Using str: %s.' % 42L
'Using str: 42.'
>>> 'Using repr: %r.' % 42L
'Using repr: 42L.'
3.3.3 字段宽度和精度
>>> '%10f ' % pi #字段宽10
' 3.141593 '
>>> '%10.2f' % pi #字段宽10,精度2
' 3.14'
>>> '%.2f' % pi #精度2
'3.14'
>>> '%.5s' % 'Guido van Rossum'
'Guido'
>>> '%.*s' % (5,'Guido van Rossum')
'Guido'
3.3.4 符号、对齐和用0填充
python笔记03:使用字符串的更多相关文章
- 我的Python笔记03
摘要: 声明:本文整理借鉴金角大王的Python之路,Day3 - Python基础3,仅供本人学习使用!!! 本节内容 1. 函数基本语法及特性 2. 参数与局部变量 3. 返回值 嵌套函数 4 ...
- Python笔记(四)_字符串的方法
字符串的方法 []表示该参数时可选的,start和end参数表示范围 count(sub[, start[, end]]) 返回sub在字符串里边出现的次数 find(sub[, start[, en ...
- Python笔记5(字符串)-20160921
一.字符串常用方法 1.title():返回字符串的标题版本,即单词首字母大写其余字母小写 2.upper():返回字符串全部大写版本,lower()则返回字符串全部小写版本 >>> ...
- Python笔记 #03# Help!
源:DataCamp datacamp 的 DAILY PRACTICE + 日常收集. Functions Built-in functions Help! Multiple arguments ...
- python笔记03
day03 一.今日内容: 1.整型 2.字符串 3.布尔类型 二.内容回顾和补充 脑图--xmind软件,processon 1.运算符补充(in.not in) value = "我是中 ...
- 【python学习笔记】3.字符串使用
[python学习笔记]3.字符串使用 字符串是一种序列,素有标准的序列操作对字符串用样适用,字符串是不可以改变 格式化操作符,%,左侧是格式化字符串,右侧是被格式的值,可以是一个值.元组.字典 数值 ...
- python笔记(2)--字符串
一.字符串 字符串是不可变序列,具有序列的公共操作方法,具体操作见python笔记(1)--序列(列表 元组 range) 1.创建字符串 单引号:'Hello , I am Logan ! ' 双引 ...
- python笔记2-数据类型:字符串常用操作
这次主要介绍字符串常用操作方法及例子 1.python字符串 在python中声明一个字符串,通常有三种方法:在它的两边加上单引号.双引号或者三引号,如下: name = 'hello' name1 ...
- python笔记(2)--字符串
一.创建字符串 使用 '' , "" , ''' 和 """ 来创建字符串 var1 = 'hello, world' var2 = "ja ...
随机推荐
- IIS Logs
日志路径 %SystemDrive%\inetpub\logs\LogFiles https://stackify.com/where-are-iis-log-files-located/ Where ...
- caffe深度学习网络(.prototxt)在线可视化工具:Netscope Editor
http://ethereon.github.io/netscope/#/editor 网址:http://ethereon.github.io/netscope/#/editor 将.prototx ...
- UVa 10118 免费糖果(记忆化搜索+哈希)
https://vjudge.net/problem/UVA-10118 题意: 桌上有4堆糖果,每堆有N颗.佳佳有一个最多可以装5颗糖的小篮子.他每次选择一堆糖果,把最顶上的一颗拿到篮子里.如果篮子 ...
- go 字符串拼接
s := "hello," m := " world" a := s + m fmt.Printf("%s\n", a)
- Windows下使用pip安装python包是报错-UnicodeDecodeError: 'ascii' codec can't decode byte 0xcb in position 0
先交待下开发环境: 操作系统:Windows 7 Python版本:2.7.9 Pip版本:6.1.1 其他环境忽略 在windows下使用pip下载python包,出现如下错误 Collecting ...
- Java注册帐号邮箱激活验证实现
Java注册帐号邮箱激活验证实现 1.需要加入发送邮件的mail.jar: http://www.oracle.com/technetwork/java/javamail/index-138643.h ...
- Codeforces 595D - Max and Bike
595D - Max and Bike 思路:开始和结束时的计时器的高度相同时(也就是关于圆竖着直径对称)时间最少. 证明: 总距离为d. 圆周长为s=2*π*r. 设len=d-floor(d/s) ...
- WPF 元素的查找
预设置元素名字 WPF有两种方式设置元素的Name <StackPanel x:Name="panel"> <Label Name="name1&quo ...
- English trip -- VC(情景课) 8 A Get ready
Words cashier # 收银员 a cashier counts money 收钱 custodian # 清洁工 a custodian cl ...
- 3-8《Ruby元编程》第二章对象模型
<Ruby元编程> 第二章 对象模型 类定义揭秘inside class definitions: class关键字更像一个作用域操作符,核心作用是可以在里面随时定义方法. [].meth ...