python学习笔记3
说明
- S格式化输出
print(format(var, '4.2f'))
print(format(var, '4.2%'))
- 强制类型转换
type(str)判断类型
强制类型转换:
age = int(age) + 1
int()
float()
- 网络编程
import socket
baiduip = socket.gethostbyname('localhost')
easy_install httplib2
import urllib
import webbrowser as web
url = 'http://www.163.com'
content = urllib.urlopen(url).read()
open('baidu.html',"w").write(content)
web.open_new_tab(baidu.html)
webbrowser.open_new_tab('http://www.baidu.com')
- 输出
反引号 print “helo “+temp
print 'the temperatrue is ' + `temp`通过反引号将temp转化为字符串
- 长字符串
当需要写一个非常长的字符串是,可以使用
sdf
Sdf
Sdf
Sdf
Sdf
s
这样可以在字符串中使用单引号和双引号。
- 普通字符串也可以跨行,如果一行之中的最后一个字符是反斜杠,那么换行本身就转意了,也就是被忽略了。
print `hello, world!`
- 反斜杠转义
path="c:\\p\\s\\d"
- 原始字符串以r开头
print r'c://python26'
- Unicode字符串前加u
u'hello, world!'