新聞中心
input()以字符串的方式獲取用戶輸入:

成都創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都做網(wǎng)站、成都網(wǎng)站制作、黃平網(wǎng)絡(luò)推廣、成都小程序開發(fā)、黃平網(wǎng)絡(luò)營銷、黃平企業(yè)策劃、黃平品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;成都創(chuàng)新互聯(lián)公司為所有大學生創(chuàng)業(yè)者提供黃平建站搭建服務(wù),24小時服務(wù)熱線:18982081108,官方網(wǎng)址:www.cdcxhl.com
>>> x = input() 4.5 >>> type(x)>>> y = input() Do you love python? >>> type(y)
輸入的字符串可以通過運算符進行連接、復(fù)制等操作:
>>> x = input() abc >>> x * 3 'abcabcabc' >>> y = input() 123 >>> x + y 'abc123'
(更多學習內(nèi)容,請點擊python學習網(wǎng))
但無法直接參與算術(shù)運算,如:
>>> x = input() 5 >>> x + 5 Traceback (most recent call last): File "", line 1, in TypeError: must be str, not int >>> x * 5 '55555' >>> y = input() 6 >>> x * y Traceback (most recent call last): File " ", line 1, in TypeError: can't multiply sequence by non-int of type 'str'
此時可以使用轉(zhuǎn)換,方法有多種:
1.指定類型轉(zhuǎn)換
1 >>> y = int(input()) 2 10 3 >>> type(y) 4
2.自動轉(zhuǎn)換
函數(shù)eval() 用來執(zhí)行一個字符串表達式,并返回表達式的值
eval(expression, globals[ ], locals[ ])
global 和 locals 分別相當于全局和局部變量,eval函數(shù)會優(yōu)先在局部變量存儲空間中檢索
1 >>> y = eval(input()) 2 4.5 3 >>> type(y) 4
3.切割轉(zhuǎn)換
利用函數(shù)split()通過指定分隔符對字符串進行切片。
str.split(str="", num=string.count(str))
str為分割符,包括空格、\n,\t 等 ,num是分割次數(shù)。
文章題目:創(chuàng)新互聯(lián)Python教程:python3下的input函數(shù)怎么用
文章地址:http://m.5511xx.com/article/dpihigj.html


咨詢
建站咨詢
