創(chuàng)新互聯(lián)www.cdcxhl.cn八線動(dòng)態(tài)BGP香港云服務(wù)器提供商,新人活動(dòng)買多久送多久,劃算不套路!
這篇文章給大家分享的是有關(guān)python中input函數(shù)的運(yùn)行方式的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考。一起跟隨小編過來看看吧。
input()以字符串的方式獲取用戶輸入:
>>> x = input() 4.5 >>> type(x) <class 'str'> >>> y = input() Do you love python? >>> type(y) <class 'str'>
輸入的字符串可以通過運(yùn)算符進(jìn)行連接、復(fù)制等操作:
>>> x = input() abc >>> x * 3 'abcabcabc' >>> y = input() 123 >>> x + y 'abc123'
但無法直接參與算術(shù)運(yùn)算,如:
>>> x = input() 5 >>> x + 5 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: must be str, not int >>> x * 5 '55555' >>> y = input() 6 >>> x * y Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: can't multiply sequence by non-int of type 'str'
此時(shí)可以使用轉(zhuǎn)換,方法有多種:
1.指定類型轉(zhuǎn)換
1 >>> y = int(input()) 2 10 3 >>> type(y) 4 <class 'int'>
2.自動(dòng)轉(zhuǎn)換
函數(shù)eval() 用來執(zhí)行一個(gè)字符串表達(dá)式,并返回表達(dá)式的值
eval(expression, globals[ ], locals[ ])
global 和 locals 分別相當(dāng)于全局和局部變量,eval函數(shù)會(huì)優(yōu)先在局部變量存儲(chǔ)空間中檢索
1 >>> y = eval(input()) 2 4.5 3 >>> type(y) 4 <class 'float'>
3.切割轉(zhuǎn)換
利用函數(shù)split()通過指定分隔符對字符串進(jìn)行切片。
str.split(str="", num=string.count(str))
str為分割符,包括空格、\n,\t 等 ,num是分割次數(shù)。
感謝各位的閱讀!關(guān)于python中input函數(shù)的運(yùn)行方式就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
分享題目:python中input函數(shù)的運(yùn)行方式-創(chuàng)新互聯(lián)
網(wǎng)頁鏈接:http://jinyejixie.com/article48/dpphep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、商城網(wǎng)站、網(wǎng)站維護(hù)、面包屑導(dǎo)航、網(wǎng)頁設(shè)計(jì)公司、網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容