Python 2.7 字符串操作 (二)

Python 2.7 字符串操作 (二)

前言

很高兴我能够坚持写第二篇文章,和大家分享更多python内容,也和大家一同学习提高自己;python应用领域很多,可以让我们在任何工作上都可以尝试使用它,让我们的工作效率提升,节省更多的时间做更多的事情;也可以让工作变的简单,让繁杂的事情简单化,所以学习python还是有很大的必要性;

对于我个人来说,我是从事编程,做Android BSP开发者;在公司除了更多技术上积累,平时也多了很多脚本化工作,繁重重复的工作越来越多,所以我下定决心想把python和shell学习一下,为后续的工作节省更多的时间;

第一章python2.7起步,初步了解了下载python,环境搭建,以及可以把python当做计算器和处理字符串;接下来,我们继续深入了解python处理字符串的更多详细的内容;

如何处理跨越多行字符串

当出现要用多行字符串时,我们可以使用"""…""" or ‘’’…’’’;

>>> print ("""\
... Usage: thingy[OPTIONS]
... -h                  Display this usage message
... -H hostname         Hostname to connect to
... """)
Usage: thingy[OPTIONS]
-h			Display this usage message
-H hostname 	Hostname to connect to

>>> 

两个或者更多的字符串文本,将自动链接在一起

>>> "I" "am" "cooper"
'Iamcooper'
>>> 

这个功能有时候还是比较有用的,当一个长字符串的句子,就可以采用此方法

>>> text = ('Put several strings within parentheses '
...         'to have them joined together.')
>>> text
'Put several strings within parentheses to have them joined together.'

但是这也有问题,这仅适用于两个文本,而不适用于变量或表达式:

>>> header = 'Hello'
>>> header 'world'
  File "<stdin>", line 1
    header 'world'
                 ^
SyntaxError: invalid syntax
>>> ('yu'*3) 'inm'
  File "<stdin>", line 1
    ('yu'*3) 'inm'
                 ^
SyntaxError: invalid syntax
>>> 

但是如何解决上面的问题呢?可以采用“+”,解决;

>>> header+'world'
'Helloworld'
>>> 

字符串索引

一个字符串,怎么表示字符串内的内容呢:比如Python,那个可以代表是“y”呢?那接下来我们看看怎么用字符串表达此问题;

从下面的例子我们可以看出字符串索引从0开始;字符串长度可以len来计算;当长度大于字符串时候会报错误IndexError: string index out of range;

>>> header = 'python'
>>> header[0]
'p'
>>> header[2]
't'
>>> header[1]
'y'
>>> header[3]
'h'
>>> header[4]
'o'
>>> header[5]
'n'
>>> header[6]           # 超过len长度报错
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> len(header)
6
>>> header[-6]
'p'
>>> header[-1]
'n'
>>> header[-2]
'o'
>>> header[-3]
'h'
>>> header[-4]
't'
>>> header[-5]
'y'
>>> header[2:5]
'tho'
>>> header[0:2]
'py'
>>> header[:2]
'py'
>>> header[3:]
'hon'
>>> 

从而我们清晰的认识到,不只是从0-5;也可以从-1 - -5 来取得字符串里的内容,也可以从某一个位置到另一个位置;

 +---+---+---+---+---+---+
 | P | y | t | h | o | n |
 +---+---+---+---+---+---+
 0   1   2   3   4   5   6
-6  -5  -4  -3  -2  -1

刚才我们超过了字符串长度取值后,发现了错误,但是我们采用另一种方法,错误就又能解决掉,我们尝试一下:

>>> header[2:6]     
'thon'
>>> header[6:]
''
>>> 

Python字符串不能更改-它们是不可变的。因此,在字符串中指定索引位置会导致错误:

>>> header[0]='o'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>> 

字符串列表

主要是list赋值,计算长度,替换,“+”,append等方法;

>>> list = [1,2,3,4,5,6,7,8]
>>> list
[1, 2, 3, 4, 5, 6, 7, 8]
>>> len(list)
8
>>> list + [1,2,3,4,5,6,7,8]
[1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8]
>>> list[0]=100
>>> list
[100, 2, 3, 4, 5, 6, 7, 8]
>>> list.append(218)
>>> list
[100, 2, 3, 4, 5, 6, 7, 8, 218]
>>> list.append(7**2)
>>> list
[100, 2, 3, 4, 5, 6, 7, 8, 218, 49]
>>> list2 = ['a','b','c']
>>> list3=[list,list2]
>>> list3
[[100, 2, 3, 4, 5, 6, 7, 8, 218, 49], ['a', 'b', 'c']]
>>> 

Unicode 字符串

>>> u'Hello world'
u'Hello world'
>>> u'Hello\u0020World !'
u'Hello World !'
>>> u'Hello World !'
u'Hello World !'
>>> ur'hello\u0020World !'
u'hello World !'
>>> ur'hello\\u0020World !'
u'hello\\\\u0020World !'
>>> u"abc"
u'abc'
>>> str(u"abc")
'abc'
>>> u"äöü"
u'\xe4\xf6\xfc'
>>> str(u"äöü")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)
>>> u"äöü".encode('utf-8')
'\xc3\xa4\xc3\xb6\xc3\xbc'
>>> unicode('\xc3\xa4\xc3\xb6\xc3\xbc', 'utf-8')
u'\xe4\xf6\xfc'
>>> 

编程while

用python做更加复杂的功能,比如做一个Fibonacci;

>>> # Fibonacci series:
... # the sum of two elements defines the next
... a, b = 0, 1
>>> while b < 10:
...     print b
...     a, b = b, a+b
...
1
1
2
3
5
8

从这个例子我们介绍了几个功能,while 循环,以及条件语句(b<10)( 其他还有 < (less than), > (greater than), == (equal to), <= (less than or equal to), >= (greater than or equal to) and != (not equal to).)

结束

已经有很多人感觉简单的操作已经不能满足自己的需求了;很急迫要写更加复杂的程序了,但是不要着急,一点点厚积薄发才是最重要的;基础牢固,才能厚积薄发;

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值