[转载] 【基础教程】Python input()函数:获取用户输入的字符串

参考链接: 从Python中控制台获取输入

input() 是 Python 的内置函数,用于从控制台读取用户输入的内容。input() 函数总是以字符串的形式来处理用户输入的内容,所以用户输入的内容可以包含任何字符。 

input() 函数的用法为: str = input(tipmsg) 

说明: str 表示一个字符串类型的变量,input 会将读取到的字符串放入 str 中。 tipmsg 表示提示信息,它会显示在控制台上,告诉用户应该输入什么样的内容;如果不写 tipmsg,就不会有任何提示信息。 

【实例】input() 函数的简单使用: a = input("Enter a number: ") b = input("Enter another number: ") print("aType: ", type(a)) print("bType: ", type(b)) result = a + b print("resultValue: ", result) print("resultType: ", type(result)) 运行结果示例: Enter a number: 100↙ Enter another number: 45↙ aType:  <class 'str'> bType:  <class 'str'> resultValue:  10045 resultType:  <class 'str'> 

↙表示按下回车键,按下回车键后 input() 读取就结束了。 

本例中我们输入了两个整数,希望计算出它们的和,但是事与愿违,Python 只是它们当成了字符串,+起到了拼接字符串的作用,而不是求和的作用。 

我们可以使用 Python 内置函数将字符串转换成想要的类型,比如: int(string) 将字符串转换成 int 类型; float(string) 将字符串转换成 float 类型; bool(string) 将字符串转换成 bool 类型。 

修改上面的代码,将用户输入的内容转换成数字: a = input("Enter a number: ") b = input("Enter another number: ") a = float(a) b = int(b) print("aType: ", type(a)) print("bType: ", type(b)) result = a + b print("resultValue: ", result) print("resultType: ", type(result)) 运行结果: Enter a number: 12.5↙ Enter another number: 64↙ aType:  <class 'float'> bType:  <class 'int'> resultValue:  76.5 resultType:  <class 'float'> 

关于 Python 2.x 上面讲解的是 Python 3.x 中 input() 的用法,但是在较老的 Python 2.x 中情况就不一样了。Python 2.x 共提供了两个输入函数,分别是 input() 和 raw_input(): Python 2.x raw_input() 和 Python 3.x input() 效果是一样的,都只能以字符串的形式读取用户输入的内容。 Python 2.x input() 看起来有点奇怪,它要求用户输入的内容必须符合 Python 的语法,稍有疏忽就会出错,通常来说只能是整数、小数、复数、字符串等。 

比较强迫的是,Python 2.x input() 要求用户在输入字符串时必须使用引号包围,这有违 Python 简单易用的原则,所以 Python 3.x 取消了这种输入方式。 

修改本节第一段代码,去掉 print 后面的括号: a = input("Enter a number: ") b = input("Enter another number: ") print "aType: ", type(a) print "bType: ", type(b) result = a + b print "resultValue: ", result print "resultType: ", type(result) 在 Python 2.x 下运行该代码: Enter a number: 45↙ Enter another number: 100↙ aType:  <type 'int'> bType:  <type 'int'> resultValue:  145 resultType:  <type 'int'>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/540617.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

程序员简历工作模式_简历的完整形式是什么?

程序员简历工作模式简历&#xff1a;简历 (CV: Curriculum Vitae) The CV is an abbreviation of Curriculum Vitae. It is a written outline summary of a persons educational training and qualifications and his other experiences. It is an absolute profile of a cand…

[转载] Python新手写出漂亮的爬虫代码1——从html获取信息

参考链接&#xff1a; Python中从用户获取多个输入 Python新手写出漂亮的爬虫代码1 初到大数据学习圈子的同学可能对爬虫都有所耳闻&#xff0c;会觉得是一个高大上的东西&#xff0c;仿佛九阳神功和乾坤大挪移一样&#xff0c;和别人说“老子会爬虫”&#xff0c;就感觉特别…

在Scala中设置&()方法

Scala中的Set&#xff06;()方法 (The Set &() method in Scala) The &() method in the Set is used to create a new set in Scala. This new set created contains all elements from the other two sets that are common for both of the given sets i.e. new set …

[转载] python与c/c++相比有哪些优势

参考链接&#xff1a; Python输入和C, Java速度对比 理论上&#xff0c;python的确比C/C慢&#xff08;我对Java的开发没有经验&#xff0c;无法评论&#xff09;。这一点不用质疑。 C/C是编绎语言&#xff0c;直接使用的是机器指令&#xff0c;而python总是跑在的虚拟机上&am…

清空日志的三种方法

方法一&#xff1a;echo "" >test.log方法二&#xff1a;> test.log方法三&#xff1a;cat /dev/null >test.log转载于:https://www.cnblogs.com/liang545621/p/7528509.html

splat net_Ruby中的Splat参数

splat netRuby Splat参数 (Ruby Splat Arguments) We have learnt how to work with methods in Ruby? We are very well aware of the fact that methods may or may not consume any arguments. Let us discuss the methods which consume argument or have a predefined ar…

ajax的访问 WebService 的方法

转自原文 ajax的访问 WebService 的方法 如果想用ajax进行访问 首先在web.config里进行设置 添加在 <webServices> <protocols> <add name "HttpPost" /> <add name "HttpGet" /> </protocols> </webServices> <s…

[转载] 使用DirectInput进行交互

参考链接&#xff1a; input()函数中的漏洞– Python2.x 使用DirectInput进行交互&#xff08;1&#xff09; DirectX 2008-08-10 15:11:34 阅读169 评论0 字号&#xff1a;大 中 小 订阅 输入设备简介 计算机通常使用三种输入设备&#xff1a;键盘、鼠标和游…

c语言 nan 常量_NaN32常量(Julia)

c语言 nan 常量Julia| NaN32常数 (Julia | NaN32 Constant) NaN32 is a constant of the Float32 type in Julia programming language, it represents "not-a-number" value. NaN32是Julia编程语言中Float32类型的常量&#xff0c;它表示“非数字”值。 Syntax: 句…

Hyperledger Fabric 1.0 从零开始(七)——启动Fabric多节点集群

5&#xff1a;启动Fabric多节点集群 5.1、启动orderer节点服务 上述操作完成后&#xff0c;此时各节点的compose配置文件及证书验证目录都已经准备完成&#xff0c;可以开始尝试启动多机Fabric集群。 首先启动orderer节点&#xff0c;切换至orderer.example.com服务器&#xff…

[转载] python中print()函数的用法和end=““不换行详解

参考链接&#xff1a; Python | print()中的结束参数 需求&#xff1a;打印五个字符&#xff0c;在一行上 代码&#xff1a; i 0 while i< 5 : i 1 print(i,end’’) 结果&#xff1a; 1 2 3 4 5那么问题来了&#xff0c;为什么加一个end"" 就不换…

css中图片左右边距_CSS中的边距

css中图片左右边距CSS保证金属性 (CSS margin property) CSS Margins are used to space around any element, for this we use "margin" property in the CSS. CSS边距用于在任何元素之间留出空间&#xff0c;为此&#xff0c;我们在CSS中使用“ margin”属性 。 S…

js 实现网页显示倒计时

用 js 来实现网页显示倒计时效果 1 function checkTime( time ){2 var data new Data(); // 获取现在时间3 var nowData data.getTime(); // 转化成毫秒数4 var time ; // 结束的时间5 var t time - nowData ;6 var HH, mm , ss 0;7 var sta "…

scala方法中的变量_Scala中的变量

scala方法中的变量Scala变量 (Scala variables) A variable is named a reference to a memory location. The location stores the data that is used by the program. 变量被称为对存储位置的引用。 该位置存储程序使用的数据。 Based on the data type of the variable the…

[转载] python[1]-print中的sep、end参数

参考链接&#xff1a; Python | print()中的sep参数 读示例程序代码时遇到的问题&#xff0c;看不懂end和sep参数。经过查找&#xff0c;基本弄清楚了。 sep&#xff1a;可以设置print中分割不同值的形式。应该是separation的缩写。 end&#xff1a;可以设置print打印结束时最…

分区 主分区 和 扩展分区_等和分区

分区 主分区 和 扩展分区Description: 描述&#xff1a; This is a popular interview coding problem which has been featured in interview rounds of Amazon, Oyo rooms, Adobe. 这是一个受欢迎的采访编码问题&#xff0c;已在亚马逊&#xff0c;Oyo房间&#xff0c;Adobe…

ORACLE 物理读 逻辑读 一致性读 当前模式读总结浅析

在ORACLE数据库中有物理读&#xff08;Physical Reads&#xff09;、逻辑读&#xff08;Logical Reads&#xff09;、一致性读&#xff08;Consistant Get&#xff09;、当前模式读&#xff08;DB Block Gets&#xff09;等诸多概念&#xff0c;如果不理解或混淆这些概念的话&a…

[转载] Java Formatter toString()方法与示例

参考链接&#xff1a; Python | 输出格式化 output format 格式化程序类toString()方法 (Formatter Class toString() method) toString() method is available in java.util package. toString()方法在java.util包中可用。 toString() method is for the string representat…

arm tbh_TBH的完整形式是什么?

arm tbhTBH&#xff1a;说实话 (TBH: To Be Honest) TBH is an abbreviation of "To Be Honest". It is internet slang which generally used as an acronym or hashtag over the internet on social media networking sites like Facebook, Instagram, Twitter, Yo…

异常:fatal: unable to access 'https://git.oschina.net/pcmpcs/library.git/': Could not resolve host...

git fork项目时出现的异常. 原因: 我以前用的是ssh地址做的远程通信地址&#xff0c;而这次是用的是https&#xff0c;因为很久没用&#xff0c;所以忘记了以前是用ssh的了。解决方案一&#xff1a;复制ssh协议的地址&#xff0c;然后再关联远程仓库。并且在VCS下的git下的Rem…