>>> s =
"mary had a little lamb"
>>> s[0]
# index is zero-based
'm'
>>> s[3] =
'x'
# changing element in-place fails
Traceback (innermost last):
File
"<stdin>", line 1,
in
?
TypeError: object doesn't support item assignment
>>> s[11:18]
# 'slice' a subsequence
'little '
>>> s[:4]
# empty slice-begin assumes zero
'mary'
>>> s[4]
# index 4 is not included in slice [:4]
' '
>>> s[5:-5]
# can use "from end" index with negatives
'had a little'
>>> s[:5]+s[5:]
# slice-begin & slice-end are complimentary
'mary had a little lamb'
>>> 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
>>> 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
>>> s3 =
"this \n and \n that"
>>>
print
s3
this
and
that
>>> s4 = r
"this \n and \n that"
>>>
print
s4
this \n
and
\n that
fh = open(
'c:\\autoexec.bat')
for
line
in
fh.readlines():
print
line
>>>
import
cStringIO
>>> fh = cStringIO.StringIO()
>>> fh.write(
"mary had a little lamb")
>>> fh.getvalue()
'mary had a little lamb'
>>> fh.seek(5)
>>> fh.write(
'ATE')
>>> fh.getvalue()
'mary ATE a little lamb'
>>>
import
string
>>> string.whitespace
'\011\012\013\014\015 '
>>> string.uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>>
import
string
>>> s =
"mary had a little lamb"
>>> string.capwords(s)
'Mary Had A Little Lamb'
>>> string.replace(s,
'little',
'ferocious')
'mary had a ferocious lamb'
>>>
import
string
>>> s =
"mary had a little lamb"
>>> string.find(s,
'had')5>>> string.count(s,
'a')4
>>>
import
string>>> s =
"mary had a little lamb"
>>> L = string.split(s)
>>> L
[
'mary',
'had',
'a',
'little',
'lamb']
>>> string.join(L,
"-")
'mary-had-a-little-lamb'
>>>
import
re
>>> s =
"mary had a little lamb"
>>>
if
re.search(
"m", s):
print
"Match!"
# char literal
Match!
>>>
if
re.search(
"[@A-Z]", s):
print
"Match!"
# char class
...
# match either at-sign or capital letter
...
>>>
if
re.search(
"\d", s):
print
"Match!"
# digits class
...
ABC1234567890XYZ ABCd12e1f37g3XYZ ABC1XYZ
ABC123456789dXYZ ABCdefghijklmnopqrstuvwXYZ ABcd12e1f37g3XYZ ABC12345%67890XYZ ABCD12E1F37G3XYZ
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有