代码改变世界

[python]print简单用法和读取用户输入

2015-11-20 15:08  sophia194910  阅读(386)  评论(0编辑  收藏  举报

代码开发环境:Eclipse

1.打印字符串:

print "Hello world!"

myString = "Hello world!"
print myString

运行结果:

Hello world!
Hello world!

 2.格式化字符串

print "%s is in the left of railway station %d" % ("huizhi", 2)

运行结果:

huizhi is in the left of railway station 2

 3.读取用户输入

输入字符串:

user = raw_input('Enter login name: ')
print "Your login name is: ", user

运行结果:

Enter login name: root
Your login name is:  root

 

输入数字:

num = raw_input('Now enter a number: ')
print 'Doubling your number: %d' % (int(num) * 2)

运行结果:

Now enter a number: 1024
Doubling your number: 2048

 注意:

需要利用内建函数int()将数值字符串转换成整数值,然后才可以对它进行数学运算。