Python学习之字符串

# 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
转换表

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值