Python学习之字符串
2015-08-01 20:32
525 查看
# coding=utf-8
# %s为占位说明符
print '字符串格式化:'
format = 'Hello, %s, %s emouth for you'
values = ('world', 'Hot')
print format % values
from math import pi
format = 'Pi with three decimals:%.3f'
print format % pi
print '%s plus %s equals %s' % (1, 1, 2)
print 'Price of eggs: $%d' % 42
print 'Hexadecimal price of eggs: %x' % 42
from math import pi
print 'Pi: %f...' % pi
print 'Very inexact estimate of pi: %i' % pi
print 'Using str:%s' % 42L
print 'Using repr :%r' % 42L
print '宽度和精度'
print '%10f' % pi
# 宽度为10,精度为2
print '%10.2f' % pi
print '%.2f' % pi
print '%.5s' % 'Guido van Rossum'
print '%.*s' % (5, 'Guido van Rossum')
print '符号,对齐,0填充'
# 用0填充
print '%010.2f' % pi
#减号(-)左对齐
print '%-10.2f' % pi
print '%5d' % 10
print '%5d' % -10
#不管正数还是负数都标出符号
print '%+5d' % 10
print '%+5d' % -10
print '字符串常量'
import string
print string.digits
print string.letters
print string.lowercase
print string.printable
print string.punctuation
print string.uppercase
print '字符串方法'
title = "Monty Python's Flying Circus"
print title.find('Monty')
print title.find('Monty', 1)
print title.find('Python', 1, 26)
#join必须是字符串
dirs = '', 'usr', 'bin', 'env'
print '/'.join(dirs)
print 'asdaTTDdddssdT'.lower()
print 'This is a test'.replace('is', 'XX')
print '1+2+3+4+5'.split('+')
print ' begin end '.strip()
print '转换表'
from string import maketrans
table = maketrans('cs', 'kz')
print table
#字母c和s分别替换成kz
print 'this is an incredible test'.translate(table)
输出
"D:\Program Files\Python\python.exe" G:/HelloPython/list/char.py
字符串格式化:
Hello, world, Hot emouth for you
Pi with three decimals:3.142
1 plus 1 equals 2
Price of eggs: $42
Hexadecimal price of eggs: 2a
Pi: 3.141593...
Very inexact estimate of pi: 3
Using str:42
Using repr :42L
宽度和精度
3.141593
3.14
3.14
Guido
Guido
符号,对齐,0填充
0000003.14
3.14
10
-10
+10
-10
字符串常量
0123456789
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
ABCDEFGHIJKLMNOPQRSTUVWXYZ
字符串方法
0
-1
6
/usr/bin/env
asdattddddssdt
ThXX XX a test
['1', '2', '3', '4', '5']
begin end
转换表
# %s为占位说明符
print '字符串格式化:'
format = 'Hello, %s, %s emouth for you'
values = ('world', 'Hot')
print format % values
from math import pi
format = 'Pi with three decimals:%.3f'
print format % pi
print '%s plus %s equals %s' % (1, 1, 2)
print 'Price of eggs: $%d' % 42
print 'Hexadecimal price of eggs: %x' % 42
from math import pi
print 'Pi: %f...' % pi
print 'Very inexact estimate of pi: %i' % pi
print 'Using str:%s' % 42L
print 'Using repr :%r' % 42L
print '宽度和精度'
print '%10f' % pi
# 宽度为10,精度为2
print '%10.2f' % pi
print '%.2f' % pi
print '%.5s' % 'Guido van Rossum'
print '%.*s' % (5, 'Guido van Rossum')
print '符号,对齐,0填充'
# 用0填充
print '%010.2f' % pi
#减号(-)左对齐
print '%-10.2f' % pi
print '%5d' % 10
print '%5d' % -10
#不管正数还是负数都标出符号
print '%+5d' % 10
print '%+5d' % -10
print '字符串常量'
import string
print string.digits
print string.letters
print string.lowercase
print string.printable
print string.punctuation
print string.uppercase
print '字符串方法'
title = "Monty Python's Flying Circus"
print title.find('Monty')
print title.find('Monty', 1)
print title.find('Python', 1, 26)
#join必须是字符串
dirs = '', 'usr', 'bin', 'env'
print '/'.join(dirs)
print 'asdaTTDdddssdT'.lower()
print 'This is a test'.replace('is', 'XX')
print '1+2+3+4+5'.split('+')
print ' begin end '.strip()
print '转换表'
from string import maketrans
table = maketrans('cs', 'kz')
print table
#字母c和s分别替换成kz
print 'this is an incredible test'.translate(table)
输出
"D:\Program Files\Python\python.exe" G:/HelloPython/list/char.py
字符串格式化:
Hello, world, Hot emouth for you
Pi with three decimals:3.142
1 plus 1 equals 2
Price of eggs: $42
Hexadecimal price of eggs: 2a
Pi: 3.141593...
Very inexact estimate of pi: 3
Using str:42
Using repr :42L
宽度和精度
3.141593
3.14
3.14
Guido
Guido
符号,对齐,0填充
0000003.14
3.14
10
-10
+10
-10
字符串常量
0123456789
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
ABCDEFGHIJKLMNOPQRSTUVWXYZ
字符串方法
0
-1
6
/usr/bin/env
asdattddddssdt
ThXX XX a test
['1', '2', '3', '4', '5']
begin end
转换表
相关文章推荐
- python 的基本语法
- 安装python
- 【python自制】让大白成为你的个人助手!
- Python中导入子文件夹中的模块
- python 去扫域名是否被注册
- 关于Python模块,或许是这样的:
- 基于物品的协同过滤算法和SlopeOne的python实现
- pyenv 安装说明
- 关于Python模块,或许是这样的:
- Python 学习之列表和元组
- 【转】Python-__builtin__与__builtins__的区别与关系(超详细,经典)
- [转]python单元测试unittest
- [python 笔记1] 输入输出
- yaml和python关系实例
- Python正则表达式指南
- python爬虫(二)--了解deque
- Python进阶安装第三方模块
- python2.7下的windows安装virtualenv和flask
- python 导入mysql乱码
- [python]字符串的排序