python
1,>>> print r"\n"
\n
r:表示原样输出
2,print u"acsd"
u:代表udecoding
3,[]表示php中的数组
也可以这样: a[-1]
a[0:2] 从0到2的位置不包括2位置
拼接使用 + 号
占位符: %s 字符串 %d int类型
4,元组占位符
>>> print "my name is %s and %s now" % ("tom","lily")
my name is tom and lily now
5,优秀的拼接方案 不用+
"".join([a,b,c])
",".join([a,b,c])
6,读写文件
写:
d = open('文件名','打开方式')
r,w,a
d.write('hi.\n second hi.')
d.close() //关闭文件
读:
print d.readline() //读取一行
读取完后,指针游标会往下,所以再次读取时候需要将游标归0
d.seek(0)
print d.read(1000) //游标归零后又可以读取了
视频7:
1,不可变数据类型
string,int tuple元组
2,字符串不可变
3,可变数据类型
dict,list[]
4,序列
单行字符串 print "this is' "
print 'this is 2"'
--------------------------------------------
3个双引号:就可以写多行字符串了
-------------------------------------------
[huyi@localhost python]$ vim a.py
#coding=utf-8
d= "中文".decode('utf-8')
print len(d)
print """
'hahha'
"随便多少都yes"
"""
-----------------------------------------
[huyi@localhost python]$ python a.py
'hahha'
5,如何修改字符串
replace
a.replace("需要修改的值","需要修改成理想的值")
>>> a="this is that"
>>> a.replace("this","blog")
'blog is that'
find
a.find("需要查询的值") //打印出的是值在字符串中的起始位置
6,占位符 %s $d
a = "this is a %s %s" % (4,3) //如果有两个占位符的话,需要使用括号括起来
print a
元组 ()
format的使用 最好使用内置的函数format 比占位符好
使用一
b = "this is { } { }".format ("my","apple")
print b
其中{} {} 里面可以填写参数的位置 {1} {0}
apple my
使用二
c = "this is {a1} {a2}".format (a1="apple",a2="my")
print c
使用三
d = "this is %{a1}s %{a2}s" % ('a1':'my','a2':'apple')
print d
推荐format
7,打开文件 另外一种放方法
import linecache 导入这个模块
linecache.getline( "a.txt" , 1 )
linecache.getline( "a.txt" , 1 )
读取第几行
lines = linecache.getlines("a.txt")
1,>>> print r"\n"
\n
r:表示原样输出
2,print u"acsd"
u:代表udecoding
3,[]表示php中的数组
也可以这样: a[-1]
a[0:2] 从0到2的位置不包括2位置
拼接使用 + 号
占位符: %s 字符串 %d int类型
4,元组占位符
>>> print "my name is %s and %s now" % ("tom","lily")
my name is tom and lily now
5,优秀的拼接方案 不用+
"".join([a,b,c])
",".join([a,b,c])
6,读写文件
写:
d = open('文件名','打开方式')
r,w,a
d.write('hi.\n second hi.')
d.close() //关闭文件
读:
print d.readline() //读取一行
读取完后,指针游标会往下,所以再次读取时候需要将游标归0
d.seek(0)
print d.read(1000) //游标归零后又可以读取了
视频7:
1,不可变数据类型
string,int tuple元组
2,字符串不可变
3,可变数据类型
dict,list[]
4,序列
单行字符串 print "this is' "
print 'this is 2"'
--------------------------------------------
3个双引号:就可以写多行字符串了
-------------------------------------------
[huyi@localhost python]$ vim a.py
#coding=utf-8
d= "中文".decode('utf-8')
print len(d)
print """
'hahha'
"随便多少都yes"
"""
-----------------------------------------
[huyi@localhost python]$ python a.py
'hahha'
5,如何修改字符串
replace
a.replace("需要修改的值","需要修改成理想的值")
>>> a="this is that"
>>> a.replace("this","blog")
'blog is that'
find
a.find("需要查询的值") //打印出的是值在字符串中的起始位置
6,占位符 %s $d
a = "this is a %s %s" % (4,3) //如果有两个占位符的话,需要使用括号括起来
print a
元组 ()
format的使用 最好使用内置的函数format 比占位符好
使用一
b = "this is { } { }".format ("my","apple")
print b
其中{} {} 里面可以填写参数的位置 {1} {0}
apple my
使用二
c = "this is {a1} {a2}".format (a1="apple",a2="my")
print c
使用三
d = "this is %{a1}s %{a2}s" % ('a1':'my','a2':'apple')
print d
推荐format
7,打开文件 另外一种放方法
import linecache 导入这个模块
linecache.getline( "a.txt" , 1 )
linecache.getline( "a.txt" , 1 )
读取第几行
lines = linecache.getlines("a.txt")