关闭

Python入门:字符串处理

标签: python
322人阅读 评论(0) 收藏 举报
分类:
>>> ord
<built-in function ord>
>>> ord('a')
97
>>> ord('*')
42
>>> ord('+')
43
>>> ord('-')
45
>>> ord('=')
61
>>> help(ord)
Help on built-in function ord in module __builtin__:

ord(...)
    ord(c) -> integer

    Return the integer ordinal of a one-character string.
#返回一个按单一字符排顺的整数
>>> ord('a')
97
>>> ord('aa')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: ord() expected a character, but string of length 2 found
#chr与ord相反
>>> chr(56)
'8'
>>> a = 'abc'
>>> print(a[:])
abc
>>> print(a[:1])
a
>>> print(a[:3])
abc
>>> print(a[:4])
abc
>>> print(a[-1:])
c
>>> print(a[-2:])
bc
>>> a.rfind('b')
1
>>> a.rfind('c')
2
>>> a.rfind('1')
-1
>>> a.rfind('19')
-1
>>> a = 'abc.txt'
>>> a.rfind('.')
3
>>> a[a.rfind('.')+1:]
'txt'
>>> a.find('e')
-1
>>> a.rfind('e')
-1
>>> help(a.rfind)
Help on built-in function rfind:

rfind(...)
    S.rfind(sub [,start [,end]]) -> int

    Return the highest index in S where substring sub is found,
    such that sub is contained within s[start:end].  Optional
    arguments start and end are interpreted as in slice notation.

    Return -1 on failure.

>>> help(a.find)
Help on built-in function find:

find(...)
    S.find(sub [,start [,end]]) -> int

    Return the lowest index in S where substring sub is found,
    such that sub is contained within s[start:end].  Optional
    arguments start and end are interpreted as in slice notation.

    Return -1 on failure.

>>> a.find('a')
0
>>> a.find('b')
1
>>> a.find('c')
2
>>> a.find('t')
4
>>> a.rfind('t')
6
>>> a.index('a')
0
>>> a.index('t')
4
>>> a.index('t')
4
>>> a.rindex('t')
6
>>> help(a.index)
Help on built-in function index:

index(...)
    S.index(sub [,start [,end]]) -> int

    Like S.find() but raise ValueError when the substring is not found.

>>> a.rindex('tt')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
ValueError: substring not found
>>> a = 'aBccDE'
>>> a.capitalize()
'Abccde'
>>> a.lower()
'abccde'
>>> a.upper()
'ABCCDE'
>>> a.swapcase()
'AbCCde'
>>> a.title()
'Abccde'
>>> help(a.title())
problem in Abccde - <type 'exceptions.ImportError'>: No module named Abccde

>>> help(a.title)
Help on built-in function title:

title(...)
    S.title() -> string

    Return a titlecased version of S, i.e. words start with uppercase
    characters, all remaining cased characters have lowercase.

>>> help(a.capitalize)
Help on built-in function capitalize:

capitalize(...)
    S.capitalize() -> string

    Return a copy of the string S with only its first character
    capitalized.

>>> a.center(10)
'  aBccDE  '
>>> a.center(10,'!')
'!!aBccDE!!'
>>> help(a.center)
Help on built-in function center:

center(...)
    S.center(width[, fillchar]) -> string

    Return S centered in a string of length width. Padding is
    done using the specified fill character (default is a space)

>>> a.ljust(2)
'aBccDE'
>>> a.ljust(20)
'aBccDE              '
>>> a.rjust(20)
'              aBccDE'
>>> print('{0}like{20}'.format('slg','python'))
Traceback (most recent call last):
  File "<input>", line 1, in <module>
IndexError: tuple index out of range
>>> print('{0}like{1}'.format('slg','python'))
slglikepython
>>> print('{0} like {1}'.format('slg','python'))
slg like python
>>> print('{} like {}'.format('slg','python'))
Traceback (most recent call last):
  File "<input>", line 1, in <module>
ValueError: zero length field name in format
>>> print('{a} like {b}'.format(a='slg',b='python'))
slg like python
>>> url = 'www.baidu.com'
>>> s = url.partition('.')
>>> s
('www', '.', 'baidu.com')
>>> print s
('www', '.', 'baidu.com')
>>> print s[0]
www
>>> print s[1]
.
>>> print s[3]
Traceback (most recent call last):
  File "<input>", line 1, in <module>
IndexError: tuple index out of range
>>> print s[2]
baidu.com
>>> s = url.rpartition('.')
>>> s
('www.baidu', '.', 'com')
>>> s = 'www.baidu.com'
>>> s.replace('com','cn')
'www.baidu.cn'
>>> a = ''.maketrans('abcc','3008')
>>> a
{97: 51, 98: 48, 99: 56}
>>> print(a)
{97: 51, 98: 48, 99: 56}
>>> print('bbbbcccc'.translate(a))
00008888

同时建议初学都使用pychram等类似IDE工具,因为在你输入的过程中会有提示,会很好的帮助你理解当前的属性、方法等
这里写图片描述

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:146835次
    • 积分:2758
    • 等级:
    • 排名:第13531名
    • 原创:122篇
    • 转载:25篇
    • 译文:10篇
    • 评论:18条
    个人独立博客
    最新评论