WEB开发网
开发学院软件开发Python 可爱的 Python:Python中的文本处理 阅读

可爱的 Python:Python中的文本处理

 2007-03-29 12:01:33 来源:WEB开发网   
核心提示: 另一个功能强大的字符串操作就是简单的 in 关键字,它提供了两个直观有效的构造:in 关键字>>> s ="mary had a little lamb">>>for cin s[11:18]:print c,# print each

另一个功能强大的字符串操作就是简单的 in 关键字。它提供了两个直观有效的构造:

in 关键字

>>> s =
    "mary had a little lamb"
>>>
    
     for
     c
    
     in
     s[11:18]:
    
     print
     c,
    # print each char in slice
...
l i t t l e
>>>
    
     if
    
    'x'
    
     in
     s:
    
     print
    
    'got x'
    # test for char occurrence
...
>>>
    
     if
    
    'y'
    
     in
     s:
    
     print
    
    'got y'
    # test for char occurrence
...
got y

在 Python 中,有几种方法可以构成字符串文字。可以使用单引号或双引号,只要左引号和右引号匹配,常用的还有其它引号的变化形式。如果字符串包含换行符或嵌入引号,三重引号可以很方便地定义这样的字符串,如下例所示:

三重引号的使用

>>> s2 =
    """Mary had a little lamb
... its fleece was white as snow
... and everywhere that Mary went
... the lamb was sure to go"""
>>>
    
     print
     s2
Mary had a little lamb
its fleece was white as snow
    
     and
     everywhere that Mary went
the lamb was sure to go

上一页  1 2 3 4 5 6 7  下一页

Tags:可爱 Python Python

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接