python的输入输出方式

本文通过实例演示了Python的基本操作,包括变量定义、数据类型转换、格式化输出及用户输入等内容。展示了如何进行数值运算、字符串处理,并介绍了不同数据类型的转换方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用代码显示:

>>> x=20
>>> x
20
>>> print x
20
>>> print "hello world"
hello world
>>> y = 30
>>> z = x + y
>>> print z
50
>>> str1 = raw_input("input str:")
input str:123dkfiasdnfg
>>> print str1
123dkfiasdnfg
>>> n = len(str1)
>>> print n
13
>>> print(format(12.23434),'6.3f')
('12.23434', '6.3f')
>>> print(format(12.23434),'6.3f'))
SyntaxError: invalid syntax
>>> print(format(12.23434,'6.3f'))
12.234
>>> print(format(12.23434,'6.9f'))
12.234340000
>>> print(format(12.23434,'6.0f'))
    12
>>> print(format(12.23434,'9.2f'))
    12.23
>>> print(format(12.23434,'3.3f'))
12.234
>>> print(format(0.3456,'.2%'))
34.56%
>>> print(format(0.3456,'3.1%'))
34.6%
>>> print(format(0.3456,'6.1%'))
 34.6%
>>> help(raw_input)
Help on built-in function raw_input in module __builtin__:

raw_input(...)
    raw_input([prompt]) -> string
    
    Read a string from standard input.  The trailing newline is stripped.
    If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.
    On Unix, GNU readline is used if enabled.  The prompt string, if given,
    is printed without a trailing newline before reading.

>>> str2 = raw_input()
woshihaoren
>>> print str2
woshihaoren
>>> type(str2)
<type 'str'>
>>> age = raw_input(your age:)
SyntaxError: invalid syntax
>>> age = raw_input("your age:")
your age:19
>>> type(age)
<type 'str'>
>>> age = age+1

Traceback (most recent call last):
  File "<pyshell#29>", line 1, in <module>
    age = age+1
TypeError: cannot concatenate 'str' and 'int' objects
>>> age = int(age)
>>> age = age +1
>>> age
20
>>> f1 = float('12.35')
>>> type(f1)
<type 'float'>
>>> print f1
12.35
>>> weight = float(raw_input("please input your weight:"))
please input your weight:23.14
>>> type(weight)
<type 'float'>
>>> print x,f1
20 12.35

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值