切换风格

Wizard Sky California Sunset glow Black Cloud Beige Dragon Lavender NewYear City Snow Flowers London
收藏本站XSS平台字符串转换jsfuck
【Love Python】【04】打通任督二脉之基本数据类型(续)[复制链接]
发表于 2014-4-7 21:06:43 | 显示全部楼层 |!read_mode!
1,如何修改字符串之replace,find
            >>> a = 'this is world'
            >>> a.replace('this','that')
            'that is world'
            >>> a
            'this is world'
            >>> a = a.replace('this','that') #记得是新建,因为字符串不可变
            >>> a
            'that is world'
            >>> 

            >>> a
            'that is world'
            >>> a.find('o')
            9                    #找到返回位置
            >>> a.find('x')
            -1                    #找不到返回-1
            >>> 

2,格式化细究
        1 % 格式化方式(查看上篇文档内容)

        2 format格式化方式
            >>> a = 'this is %s %s' % ('my','apple')
            >>> a
            'this is my apple'
            >>> b = 'this is {1} {0}' .format('apple','my')
            >>> b
            'this is my apple'
            >>> c = 'this is {whose} {fruit}' .format (fruit='apple',whose='my')
            >>> c
            'this is my apple'
            >>> 

        3 为什么要用format
            好管理

        4 还有一个方法,字典来了。(初步了解下字典)
            >>> a = 'this is %(whose)s %(fruit)s' % {'whose':'my','fruit':'apple'}
            >>> a
            'this is my apple'
            >>> 

3,再议读写文件(初识标准库模块)
            >>> a = open('tmp.txt','w')
            >>> a.write('hahahah\nhahhha')
            >>> a.close()
            >>> import linecache
            >>> print linecache.getline('tmp.txt',1)
            hahahah
            
            >>> print linecache.getline('tmp.txt',2)
            hahhha
            
            >>> lines = linecache.getline('tmp.txt')
            Traceback (most recent call last):
              File "<stdin>", line 1, in <module>
            TypeError: getline() takes at least 2 arguments (1 given)
            >>> lines = linecache.getlines('tmp.txt')
            >>> print lines
            ['hahahah\n', 'hahhha\n']
            >>> 

查看linecache标准库模块linecache代码
help(linecache)


Help on module linecache:

NAME
    linecache - Cache lines from files.

FILE
    /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/linecache.pyc

MODULE DOCS
    http://docs.python.org/library/linecache

DESCRIPTION
    This is intended to read lines from modules imported -- hence if a filename
    is not found, it will look down the module search path for a file by
    that name.

FUNCTIONS
    checkcache(filename=None)
        Discard cache entries that are out of date.
        (This is not checked upon each call!)
   
    clearcache()
        Clear the cache entirely.
:

FILE即为此标准库模块的位置不同的系统和版本可能位置不同,以自己的为准

小题一道
a = 't00ls'
b = 'apple'
用字典和format方法实现:

my name is t00ls, i love apple.

另外如果文档中涉及的内容有错误或不当之处还望多多指教:)
友情提示本文档为连载系列:) 抚菊深思@t00ls.net  店小二01@psacms.com

操千曲而后晓声,观千剑而后识器。
发表于 2014-4-11 15:31:55 | 显示全部楼层
a = 't00ls'
b = 'apple'
c='my name is {0}, i love {1}.'.format(a,b)
print c
d='my name is %(0)s, i love %(1)s.'%{'0':a,'1':b}
print d

代码区

GMT+8, 2019-11-22 01:16

Powered by Discuz! X2

© 2001-2018 Comsenz Inc.

回顶部