raw_input() and input() in python

本文探讨了Python中两种常见的输入函数:input() 和 raw_input() 的区别。通过实例演示了这两种函数如何处理不同类型的数据输入,并解释了它们在数据类型转换方面的不同之处。

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


there are two common input function,it's raw-input() and input(),thier function is receive the input from console ,but they have received and operated the data ,which is different. Then let's discuss the different of them.

#! usr/bin/env python
# coding = utf-8
'''
this function is test input function and raw_input function,of course this also test the comments,
include single-line comments,local comments and the block comments.
'''
def fun_str():
    a = input("str_Input:") # if you input a str ,then there will report a syntax error
    print(a)
    b = raw_input("str_raw_input:")
    print(b)
def fun_int():
    a = input("int_Input:")
    print(a)
    print(str(type(a)))
    b = raw_input("int_str_raw_input:")
    print(b)
    print(str(type(b)))
#fun_str()
fun_int()
let's see the code,you  could input 123

the result is :

int_Input:123

<type 'int'>

then you could input 123

int_str_raw_input: 123

123

<type 'str'>


if you annotate the “fun_int” ,and use function fun_str():

if you could input abc

 then there will be an error ,becasue the input() requiers what you have input  is a  legitimate expression,so the "abc"

not transfer the type of int . so when you want to input "abc",you should use expression "input("abc")" or "input('abc')".


As shown above ,we can see that raw_input return a str whatever you input , but input()  return the data type that you input.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值