Python字符串和数值

纸上得来终觉浅,绝知此事要躬行。

Python字符串和数值

1. 字符串

字符串是Python中最常用的数据类型,访问子字符串,可以使用方括号来截取字符串。同时,也可以通过等号进行赋值操作。如下所示,我们可以内置函数dir方法得到其对应的属性和方法。

>>> dir('')
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__',
'__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__',
'__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__',
'__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__',
'__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize',
'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find',
'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit',
'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle',
'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition',
'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip',
'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate',
'upper', 'zfill']
  • 基本使用
In [1]: 'Hello, World!'
Out[1]: 'Hello, World!'

In [2]: 'Hello,' + ' World!'
Out[2]: 'Hello, World!'

In [3]: """
   ...: This
   ...: is
   ...: a
   ...: Test
   ...: """
Out[3]: 'nThisnisnanTestn'

In [4]: print('nThisnisnanTestn')

This
is
a
Test
  • 开头或结尾
    • startswith
    • endswith
In [5]: 'this'.startswith('t')
Out[5]: True

In [6]: 'this'.endswith('t')
Out[6]: False

In [7]: 'this'.startswith('t', 1, 3)
Out[7]: False
  • 大小写转换
    • upper
    • lower
    • swapcase
    • title
    • capitalize
In [8]: 'abc'.upper()
Out[8]: 'ABC'

In [9]: 'ABC'.lower()
Out[9]: 'abc'

In [10]: 'Abc'.swapcase()
Out[10]: 'aBC'

In [11]: 'abc'.title()
Out[11]: 'Abc'

In [12]: 'abc'.capitalize()
Out[12]: 'Abc'
  • 切分或分割
    • split
    • rsplit
In [13]: 'a b c d'.split()
Out[13]: ['a', 'b', 'c', 'd']

In [14]: 'a b c d'.split(None, 2)
Out[14]: ['a', 'b', 'c d']

In [15]: 'a,b,c,d'.split(',')
Out[15]: ['a', 'b', 'c', 'd']

In [16]: 'a,b,c,d'.split(',', 2)
Out[16]: ['a', 'b', 'c,d']

In [17]: 'a,b,c,d'.split()
Out[17]: ['a,b,c,d']

In [18]: 'a b c d'.rsplit(None, 1)
Out[18]: ['a b c', 'd']
  • 移除首尾特殊字符
    • strip
    • lstrip
    • rstrip
In [19]: ' abc '.strip()
Out[19]: 'abc'

In [20]: ' abcn'.strip()
Out[20]: 'abc'

In [21]: ' abc '.lstrip()
Out[21]: 'abc '

In [22]: ' abc '.rstrip()
Out[22]: ' abc'

In [23]: '#abc#'.strip('#')
Out[23]: 'abc'
  • 替换
    • replace
In [24]: 'aabbcc'.replace('a', 'd')
Out[24]: 'ddbbcc'

In [25]: 'aabbcc'.replace('a', 'd', 1)
Out[25]: 'dabbcc'

In [26]: 'abc'.replace('a', 'd').replace('d', 'e')
Out[26]: 'ebc'
  • 分区
    • partition
    • rpartition
In [27]: 'a/b/c'.partition('/')
Out[27]: ('a', '/', 'b/c')

In [28]: 'a/b/c'.rpartition('/')
Out[28]: ('a/b', '/', 'c')
In [29]: schame, _, url = 'http://www.baidu.com'.partition('://')

In [30]: schame
Out[30]: 'http'

In [31]: url
Out[31]: 'www.baidu.com'
  • 连接
    • join
# 不要使用'+'来连接字符串
In [32]: ' '.join([ 'I', 'love', 'python' ])
Out[32]: 'I love python'
  • 填充
    • center
    • ljust
    • rjust
    • zfill
In [33]: 'abc'.center(20)
Out[33]: '        abc         '

In [34]: 'abc'.center(20, '#')
Out[34]: '########abc#########'

In [35]: 'abc'.ljust(20)
Out[35]: 'abc                 '

In [36]: 'abc'.rjust(20)
Out[36]: '                 abc'

In [37]: 'abc'.zfill(20)
Out[37]: '00000000000000000abc'

2. 格式化

  • print
In [1]: 'I love %s' % ('python')
Out[1]: 'I love python'

In [2]: 'I love %s' % ('python',)
Out[2]: 'I love python'

In [3]: 'I love %(name)s' % { 'name':'python' }
Out[3]: 'I love python'

In [4]: 'I love %(name)s, %(name)s is my first languages' % { 'name':'python' }
Out[4]: 'I love python, python is my first languages'

In [5]: 'I love %s, %s is my first langeages' % ('python','python')
Out[5]: 'I love python, python is my first langeages'

In [6]: '%d is a number' % 4
Out[6]: '4 is a number'

In [7]: '%E' % 0.0000000000001
Out[7]: '1.000000E-13'

In [8]: '%10d' % 1
Out[8]: '         1'

In [9]: '%010d' % 1
Out[9]: '0000000001'

In [10]: ' New list is %s' % (new_list,)
Out[10]: ' New list is [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]'
  • format

python-zifuchuan

# 使用方式
[[fill]align][sign][#][0][minimumwidth][.precision][type]

# PEP3101中说明format是为了替代%这种字符串格式方法的
In [1]: 'I am {}'.format('escape')
Out[1]: 'I am escape'

In [2]: 'I am {}, age is {}'.format('escape',18)
Out[2]: 'I am escape, age is 18'

In [3]: 'I am {1}, age is {0}'.format(18, 'escape')
Out[3]: 'I am escape, age is 18'

In [4]: '{name}, age is {age}'.format(name='escape', age=18)
Out[4]: 'escape, age is 18'

In [5]: '{name}, name is {name}'.format(name='escape')
Out[5]: 'escape, name is escape'

In [6]: '{0}, name is {0}'.format('escape')
Out[6]: 'escape, name is escape'
# 使用内置数据类型的参数
In [7]: 'My {config[spam]} runs {sys.platform}'.format(sys=sys, config = {'spam': 'laptop'})
Out[7]: 'My laptop runs darwin'

# 使用函数中的方法
In [8]: "Today is: {0:%a %b %d %H:%M:%S %Y}".format(datetime.now())
Out[8]: 'Today is: Tue Jun 19 16:00:49 2018'
In [8]: "My name is {0.name}".format(open('out.txt', 'w'))
Out[8]: My name is out.txt

# !r表示repr()、!s表示str()
In [9]: "{0!r:20}".format("Hello")
Out[9]: "'Hello'             "
In [10]: "{0!s:20}".format("Hello")
Out[10]: 'Hello               '
# 用户可以自定义字符串格式化方式
class NamespaceFormatter(Formatter):
   def __init__(self, namespace={}):
       Formatter.__init__(self)
       self.namespace = namespace

   def get_value(self, key, args, kwds):
       if isinstance(key, str):
           try:
               # Check explicitly passed arguments first
               return kwds[key]
           except KeyError:
               return self.namespace[key]
       else:
           Formatter.get_value(key, args, kwds)
  • f 字符串
# 使用方式
f ' <text> { <expression> <optional !s, !r, or !a> <optional : format specifier> } <text> ... '

# 提供一种简明、可读的字符串格式化方法
In [1]: name = 'escape'

In [2]: f'He said his name is {name}.'
Out[2]: 'He said his name is escape.'

In [3]: value = decimal.Decimal('12.34567')
In [4]: f'result: {value:10.4}'
Out[4]: 'result:      12.35'
In [5]: import datetime
In [6]: anniversary = datetime.date(2018, 10, 11)
In [7]: f'my anniversary is {anniversary:%A, %B %d, %Y}.'
Out[7]: 'my anniversary is Thursday, October 11, 2018.'

3. 数值

  • 整数(int)
In [1]: 1 + 2
Out[1]: 3

In [2]: 2 - 1
Out[2]: 1

In [3]: 2 * 3
Out[3]: 6

In [4]: 2 / 3
Out[4]: 0.6666666666666666

In [5]: 3 / 2
Out[5]: 1.5

In [6]: 2 ** 3
Out[6]: 8

In [7]: 3 % 2
Out[7]: 1
  • 整数(float)
# 因为底层CPU和ieee754标准,导致不能精确的显示十进制数值
In [8]: 1.1 + 0.1
Out[8]: 1.2000000000000002

In [9]: 1.1 + 0.2
Out[9]: 1.3

In [10]: 1.1 / 0.2
Out[10]: 5.5

In [11]: 1.1 - 0.1
Out[11]: 1.0

In [12]: 1.1 - 0.2
Out[12]: 0.9000000000000001

In [13]: 1.1 * 0.1
Out[13]: 0.11000000000000001
# 精确计算可以使用Decimal模块
# 在Python3中是内置的,但在Python2中需要额外引入

In [14]: from decimal import Decimal

In [15]: Decimal('1.1') + Decimal('0.1')
Out[15]: Decimal('1.2')

In [16]: Decimal('1.1') * Decimal('0.1')
Out[16]: Decimal('0.11')
  • 布尔值(bool)
常见布尔值为 Flase 的值 说明
None 空值
False 布尔值
0 整数
0.0 浮点数
'' 空字符串
[] 空列表
() 空元组
{} 空字典
  • 类型转换
In [17]: str(1)
Out[17]: '1'

In [18]: int('1')
Out[18]: 1

In [19]: float('1')
Out[19]: 1.0

In [20]: float(1)
Out[20]: 1.0

4. 常见问题

  • 除法运算
# Python3中的除法运算
In [22]: 2 / 3
Out[22]: 0.6666666666666666

In [23]: 3 / 2
Out[23]: 1.5
# Python2中的除法运算
>>> 2 / 3
0

>>> 3 / 2
1

# 【方法一】通过转换类型解决
>>> float(3) / 2
1.5

>>> float(2) / 3
0.6666666666666666

# 【方法二】引入新除法运算
>>> from __future__ import division
>>> 3 / 2
1.5

>>> 2 / 3
0.6666666666666666
  • 精确求值
# 精确计算可以使用Decimal模块
# 在Python3中是内置的,但在Python2中需要额外引入

In [14]: from decimal import Decimal

In [15]: Decimal('1.1') + Decimal('0.1')
Out[15]: Decimal('1.2')

In [16]: Decimal('1.1') * Decimal('0.1')
Out[16]: Decimal('0.11')