python 字符串

2019-09-24  本文已影响0人  zenos876

1. 基本操作

website = 'http://www.python.org'
# 字符串是不可变的  元素赋值, 切片赋值都是不可以的
# website[-3:] = 'com'
# website[1] = 'http'
Traceback (most recent call last):
  File "c:\Users\87600\Desktop\python_basic_learning\3.using_str\1.test_str.py", line 8, in <module>
    website[-3:] = 'com'
TypeError: 'str' object does not support item assignment

2. 字符串基本格式化

2.1 转换说明符%s

print('{:=^50}'.format('2.1')) 
basic_str = 'hello, %s %s enough for ya?'
value = ('world', 'Hot')
format_str = basic_str % value
print(format_str)
=======================2.1========================
hello, world Hot enough for ya?

2.2 模板字符串

print('{:=^50}'.format('2.2')) 
from string import Template
tmpl = Template('Hello, $who! $what enough for ya?')
format_str = tmpl.substitute(who='Mars', what='Dusty')
print(format_str)
=======================2.2========================
Hello, Mars! Dusty enough for ya?

2.3 字符串方法format

# 2.3.1 没有名称
print('{:=^50}'.format('2.3.1')) 
format_str = '{}, {} and {}'.format('first', 'second', 'third')
print(format_str)
# 2.3.2 使用索引
print('{:=^50}'.format('2.3.2'))
format_str = '{0}, {1} and {2}'.format('first', 'second', 'third')
print(format_str)
format_str = '{1}, {0} and {2}, {0}'.format('first', 'second', 'third')
print(format_str)
# 2.3.3 使用字段名
print('{:=^50}'.format('2.3.3'))
format_str = '{name}, {value}'.format(name='first', value='second')
print(format_str)
# 2.3.4 直接使用变量值进行设置
print('{:=^50}'.format('2.3.4'))
a = 123456
format_str = f'a={a}'
print(format_str)
======================2.3.1=======================
first, second and third
======================2.3.2=======================
first, second and third
second, first and third, first
======================2.3.3=======================
first, second
======================2.3.4=======================
a=123456

2.4 字符串格式化高级用法

# 2.4.1 在最后结果添加花括号
print('{:=^50}'.format('2.4.1'))
format_str = '{{ hello world }}'.format()
print(format_str)

# 2.4.2 替换字段名
print('{:=^50}'.format('2.4.2'))
format_str = '{foo} {} {bar} {}'.format(1, 2, bar=4, foo=3)
print(format_str)
format_str = '{foo} {1} {bar} {0}'.format(1, 2, bar=4, foo=3)
print(format_str)

# 2.4.3 传递列表
print('{:=^50}'.format('2.4.3'))
name = ['wu', 'herry']
format_str = 'Mr {name[1]}'.format(name=name)
print(format_str)

# 2.4.4 传递字典
print('{:=^50}'.format('2.4.4'))
temp_dict = {'name': 'Wu'}
format_str = 'Mr {name}'.format(**temp_dict)
print(format_str)



# 2.4.5 传递对象
print('{:=^50}'.format('2.4.5'))
import math
format_str = 'model_name = {mod.__name__}, e = {mod.e}'.format(mod=math)
print(format_str)



# 2.4.6 基本转换    
print('{:=^50}'.format('2.4.6'))
# !s !r !a  -->  str repr ascii
print('{pi!s} {pi!r} {pi!a}'.format(pi=' Π '))
# f 浮点数
print('{num:f}'.format(num=42))
# b 二进制数
print('{num:b}'.format(num=8))
# o 八进制
print('{num:o}'.format(num=8))
# x 十六进制
print('{num:x}'.format(num=8))
# e 科学计数法表示小数
print('{num:e}'.format(num=1.233333333))


# 2.4.7  宽度, 精度和千分位
print('{:=^50}'.format('2.4.7'))
# 使用整数指定宽度
print('{num:10}'.format(num=3))
# 使用整数指定精度
print('{num:.10f}'.format(num=3))
# 千分位符
print('{num:,}'.format(num=1000**10))


# 2.4.8 符号,对齐, 用0填充
print('{:=^50}'.format('2.4.8'))
# 在指定宽度和精度前加特定符
# 用0填充
print('{num:010.3f}'.format(num=65.14))
# 正号
print('{num:+10.3f}'.format(num=65.14))
# 负号
print('{num:-10.3f}'.format(num=-65.14))
# 左对齐
print('{num:<10.3f}'.format(num=-65.14))
# 右对齐
print('{num:>10.3f}'.format(num=-65.14))
# 在^前添加填充字符串
print('{num:#^10.3f}'.format(num=-65.14))
# 在符号说明符和宽度之间加上#, 在进制前加修饰符
print('{num:#10b}'.format(num=10))
======================2.4.1=======================
{ hello world }
======================2.4.2=======================
3 1 4 2
3 2 4 1
======================2.4.3=======================
Mr herry
======================2.4.4=======================
Mr Wu
======================2.4.5=======================
model_name = math, e = 2.718281828459045
======================2.4.6=======================
 Π  ' Π ' ' \u03a0 '
42.000000
1000
10
8
1.233333e+00
======================2.4.7=======================
         3
3.0000000000
1,000,000,000,000,000,000,000,000,000,000
======================2.4.8=======================
000065.140
   +65.140
   -65.140
-65.140
   -65.140
#-65.140##
    0b1010

2.5 例子

print('{:=^50}'.format('2.5'))
width = 35
price_width = 10
item_width = width - price_width

header_fmt = '{{:{}}}{{:>{}}}'.format(item_width, price_width)
fmt = '{{:{}}}{{:>{}.2f}}'.format(item_width, price_width)

print('=' * width)
print(header_fmt)

print(header_fmt.format('Item', 'Price'))

print('=' * width)

print(fmt.format('Apple', 0.4))
print(fmt.format('Pears', 0.5))

print('=' * width)
===================================
{:25}{:>10}
Item                          Price
===================================
Apple                          0.40
Pears                          0.50
===================================

3. 字符串方法

# 3.1 center 在两边填充的字符
print('3.1'.center(50, '='))
print('a'.center(10, '!'))

# 3.2 find 查找子串
print('3.2'.center(50, '='))
# 返回第一个字串的起始索引
base_str = 'as a moo, moo like moo'
print(base_str.find('moo'))
# 查找不到 返回-1
print(base_str.find('baby'))
# 指定起点终点
print(base_str.find('moo', 6, -1))

# 3.3 join 合并序列元素
print('3.3'.center(50, '='))
seq = ['1', '2', '3']
print(''.join(seq))
# 不能合并数字列表
seq = [1, 2, 3]
# print(''.join(seq))

# 3.4 lower 返回字符串小写
print('3.4'.center(50, '='))
print('ABCsdsa'.lower())

# 3.5 title 修改所有词首的首字母大写
print('3.5'.center(50, '='))
print("This's all, herry".title())
# capwords修正
import string
print(string.capwords("This's all, herry"))

# 3.6 replace 将指定的子字符串替换为另一个字符串
print('3.6'.center(50, '='))
print('as a moo, moo like moo'.replace('moo', 'boo'))

# 3.7 split 将字符串拆分成序列
print('3.7'.center(50, '='))
print('as a moo, moo like moo'.split(','))

# 3.8 strip 去除字符串开头和末尾的特殊字符
print('3.8'.center(50, '='))
print('  hello, world !!'.strip(' '))

# 3.9 translate 替换单字符
# 创建转换表
table = str.maketrans('cs', 'kz')
# unicode码点的映射
print(table)
# 修改单字符
print('this is a incredible test'.translate(table))
# 删除指定字符
table = str.maketrans('cs', 'kz', 'a')
print('this is a incredible test'.translate(table))
=======================3.1========================
!!!!a!!!!!
=======================3.2========================
5
-1
10
=======================3.3========================
123
=======================3.4========================
abcsdsa
=======================3.5========================
This'S All, Herry
This's All, Herry
=======================3.6========================
as a boo, boo like boo
=======================3.7========================
['as a moo', ' moo like moo']
=======================3.8========================
hello, world !!
{99: 107, 115: 122}
thiz iz a inkredible tezt
thiz iz  inkredible tezt
上一篇下一篇

猜你喜欢

热点阅读