寒江独钓

博客园 首页 新随笔 联系 订阅 管理

一: 基本输入 raw_input

二: 基本输出 print

示例:

1)

  raw_input('Input your name:')

  之后需要从键盘输入一个字符串 

 也可以将输入字符串保存在一个变量中

 name=raw_input('input your name:')

2) >>>year =raw_input('The year:')

    'The year:2007 回车

   >>>print year+1

   >>>print int(year)+1

三:类型转化函数

   float         字符串或整形转浮点型

   str           数字转字符串

   chr          ascii值转ascii字符

   hex          整形转十六进制字符串

   long         字符串转长整形

   oct           整数转八进制

   ord          ascii字符转ascii值

一般用print可以打印所有的python数据类型,如果自己定义某一新的类型或类,可以重载_repr_让print语句支持。

编码实例:

# -*- coding:utf-8 -*-
#file: Chinese.py
#
chinese='''
 在python中使用中文,需要注意字符编码问题,可以使用的字符编码为
 utf-8
 cp936
 gb2312
 iso-8859-1
'''
print chinese
print chinese.decode('utf-8').encode('cp936')

posted on 2012-09-27 10:11  X.W.LIU  阅读(165)  评论(0)    收藏  举报