#!/usr/bin/env python
#-*- coding:UTF-8 -*-
#####################################################
# Author: sunfx [email protected]
# Last modified: 2014/11/11
# Filename: string.py
# Q Q : 236147801
#####################################################
import re
import string
str = 'I love python'
#1. 20
print str.center(20)
#2.
print str.ljust(20)
#3.
print str.rjust(20)
#4.str , 0
print str.zfill(20)
#5.
print str.upper()
#6.
print str.lower()
#7.
s = 'Thi quick brown fox jumped over the lazy dog.'
print s
print string.capwords(s)
#8.
print str.swapcase()
#9. , ,
print str.title()
#10. replace
leet = string.maketrans('abegiloprstz','463611092572')
s = 'Thi quick brown fox jumped over the lazy dog.'
print s
print s.translate(leet)
str = '0123'
#11. ,
print str.isalnum()
#12. ,
print str.isdigit()
#13. ,
print str.isalpha()
#14. , , True
print str.islower()
#15. ,
print str.isspace()
#16. str
print str.startswith('str')
#17. str
print str.endswith('str')
str = 'string 1EAMD'
#18. , -1,
print str.find('a')
#19. , -1,
print str.rfind('a')
#20. , ,
print str.index('s')
#21. , ,
print str.rindex('s')
#22.
print str.count('s')
#23.
print str.replace('s','A')
#24. ,
print str.strip('n')
#25.
print str.lstrip('n')
#26.
print str.rstrip('n')
#27.
str = ' tab'
print str.expandtabs()
str = ' '
#28. , utf-8 unicode
print str.decode('utf-8')
#29. , unicode gbk
print str.decode('utf-8').encode('gbk')
#30. unicode utf-8
print str.decode('utf-8').encode('utf-8')
#31.
str = 'Learn string'
print '-'.join(str)
#32.
print str.split(' ')
#33. Template
values = {'var':'foo'}
t = string.Template("""
Variale : $var
Escape : $$
Variable in text: ${var}iable
""")
print 'TEMPLATE:',t.substitute(values)
s = """
Variale : %(var)s
Escape : %%
Variable in text: %(var)siable
"""
print 'INTERPOLATION:', s % values
#34.Template
t = string.Template("$var is here but $missing is not provided")
try:
print 'substitute() :',t.substitute(values)
except KeyError, err:
print 'ERROR:',str(err)
print 'safe_substitute():',t.safe_substitute(values)
#36.
template_text = '''
Delimiter : %%
Replaced : %with_underscore
Ignored : %notundersocred
'''
#37. Templace
d = { 'with_underscore':'replaced',
'notundersocred':'not replaced'
}
class MyTemplate(string.Template):
delimiter = '%'
idpattern = '[a-z]+_[a-z]+'
t = MyTemplate(template_text)
print 'Modified ID pattern:'
print t.safe_substitute(d)
#38.
t = string.Template('$var')
print t.pattern.pattern
'''
\$(?:
(?P<escaped>\$) | # Escape sequence of two delimiters #
(?P<named>[_a-z][_a-z0-9]*) | # delimiter and a Python identifier #python ( )
{(?P<braced>[_a-z][_a-z0-9]*)} | # delimiter and a braced identifier #
(?P<invalid>) # Other ill-formed delimiter exprs #
)
'''
class MyTemplate(string.Template):
delimiter = '{{' #
pattern = r'''
\{\{(?:
(?P<escaped>\{\{) |
(?P<named>[_a-z][_a-z0-9]*)\}\} |
(?P<braced>[_a-z][_a-z0-9]*)\}\} |
(?P<invalid>)
)
'''
t = MyTemplate('''
{{{{
{{var}}
''')
print t.template # template
print 'MATCHES:',t.pattern.findall(t.template) #
print 'SUBSTIITUTED:',t.safe_substitute(var='replacement') #
print MyTemplate.delimiter # mytemplate
print string.Template.delimiter # string
#http://legacy.python.org/dev/peps/pep-0292/
#
#39. , ,
import textwrap
sample_text = '''
The textwrap modulde can be used to format text for output in
situations where pretty-parinting is desired. It offers
programmatic functionality similar to the paragraph wrapping
or filling features found in myany text editors.
'''
print 'No dedent:
'
print textwrap.fill(sample_text,width=50)
#40. ( )
dedemted_text = textwrap.dedent(sample_text)
print dedemted_text
#41. fill dedent
dedemted_text = textwrap.dedent(sample_text).strip()
print dedemted_text
for width in [45,70]:
print '%d Columns:
' % width
print textwrap.fill(dedemted_text,width=width)
print
#42.
print textwrap.fill(dedemted_text,
initial_indent='', #
subsequent_indent=' ' * 4, #
width=50 # , 75
)