python怎麼把字串轉換成數字

2021-05-29 01:10:19 字數 6022 閱讀 2747

1樓:折柳成萌

整數字符

串來轉換為對應源的整數

int('12')

小數字bai符串du轉換為對應小數

float('12.34')

數字轉換為字串

str(123.45)

ascii碼轉換為相應

zhi字元

chr(97)

字元轉dao換為響應ascii碼

ord('a')

2樓:匿名使用者

int("123") => 123

python 字元與數字如何轉換

3樓:zer0小雪

python中字元與數字相互轉換用chr()即可。

python中的字元數字之間的轉換函式

int(x [,base ])                               將x轉換為一個整數

long(x [,base ])                            將x轉換為一個長整數

float(x )                                       將x轉換到一個浮點數

***plex(real [,imag ])                  建立一個複數

str(x )                                          將物件 x 轉換為字串

repr(x )                                       將物件 x 轉換為表示式字串

eval(str )                                     用來計算在字串中的有效python表示式,並返回一個物件

tuple(s )                                      將序列 s 轉換為一個元組

list(s )                                          將序列 s 轉換為一個列表

chr(x )                                         將一個整數轉換為一個字元

unichr(x )                                    將一個整數轉換為unicode字元

ord(x )                                         將一個字元轉換為它的整數值

hex(x )                                         將一個整數轉換為一個十六進位制字串

oct(x )                                         將一個整數轉換為一個八進位制字串

chr(65)='a'

ord('a')=65

int('2')=2;

str(2)='2'

4樓:日time寸

整數字串轉換為對應的整數

int('12')

小數字串轉換為對應小數

float('12.34')

數字轉換為字串

str(123.45)

ascii碼轉換為相應字元

chr(97)

字元轉換為響應ascii碼

ord('a')

5樓:尼克的右手

一、python中字串轉換成數字(1)import string

t='555'

ts=string.atoi(tt)

ts即為tt轉換成的數字

轉換為浮點數 string.atof(tt)(2)直接int

int(tt)即可。

二、數字轉換成字串

tt=322

tem='%d' %tt

tem即為tt轉換成的字串

6樓:匿名使用者

整數字串轉換為對應的整數int('12')。

使用格式化字串:

tt=322

tem='%d' %tt

tem即為tt轉換成的字串

小數字串轉換為對應小數float('12.34')。

double num1 = 0.0;

string ** = "12.34";

num1 = double.valueof(**.tostring());

數字轉換為字串str(123.45)。

(123.45).to_bytes(4, 'big')

b'\x13\x9b\xe5\x87'

ascii碼轉換為相應字元chr(97)。

字元轉換為響應ascii碼ord('a')。

下面是python中對這幾個方法的簡單說明:ord(...) ord(c) -> integer  return the integer ordinal of a one-character string。

chr(...)

chr(i) -> character

return a string of one character with ordinal i; 0 <= i < 256。

repr(...)

repr(object) -> string return the canonical string representation of the object。

for most object types, eval(repr(object)) == object。

unichr(...)

unichr(i) -> unicode character  return a unicode string of one character with ordinal i; 0 <= i <= 0x10ffff。

7樓:匿名使用者

#x須為數字,否則把字串型別的字母/漢子/標點符號轉化為int型別毫無意義,會報錯。

x = 1 #x為int型別。

s = str(x) #把x轉換為字串型別,即'x'.

i = int(s) #把字串型別的x轉換為int型別的x。

8樓:藍藍藍

一、python中字串轉換成數字

1、類中進行匯入:import string ,str='555',num=string.atoi(str),num即為str轉換成的數字轉換為浮點數:

string.atof(str)

2、直接intint(str)即可。

二、數字轉換成字串

num=322,str='%d'%num,str即為num轉換成的字串

9樓:淺雨唯一

#!/usr/bin/python

#-*- coding:utf-8 -*-if __name__ == "__main__":

a = '64'

print 'type(a)=', type(a)print 'a=', a

print

b = int(a)

print 'type(b),', type(b)print 'b=', b

print

c = '40'

print 'type(c),', type(c)print 'c=', c

d = int(c, 16)

print 'type(d),', type(d)print 'd=', d

print

e = '100'

print 'type(e),', type(e)print 'e=', e

f = int(e, 8)

print 'type(f),', type(f)print 'f=', f

print

g = '1000000'

print 'type(g),', type(g)print 'g=', g

h = int(g, 2)

print 'type(h),', type(h)print 'h=', h

10樓:匿名使用者

int(str)

float(str)

str(int)

str(float)

11樓:名字都沒一個

將input中的字串轉換為數字:

首先我們知道inpu輸入的內容為字元,如果輸入為『』數字『』要轉換為python中的數字,則要分為整數數值,或小數點數值,可用以下方法:

a=input('請輸入一個數字')

try:

n=int(a)

except:

n=float(a)

另外如果要轉換為其他型別,可以在後面再加except:

int(x [,base ]) 將x轉換為一個整數

long(x [,base ]) 將x轉換為一個長整數

float(x ) 將x轉換到一個浮點數

***plex(real [,imag ]) 建立一個複數

str(x ) 將物件 x 轉換為字串

repr(x ) 將物件 x 轉換為表示式字串

eval(str ) 用來計算在字串中的有效python表示式,並返回一個物件

tuple(s ) 將序列 s 轉換為一個元組

list(s ) 將序列 s 轉換為一個列表

chr(x ) 將一個整數轉換為一個字元

unichr(x ) 將一個整數轉換為unicode字元

ord(x ) 將一個字元轉換為它的整數值

hex(x ) 將一個整數轉換為一個十六進位制字串

oct(x ) 將一個整數轉換為一個八進位制字串

ord: 將ascii字串轉化為ascii值

python怎麼把字串轉換成數字

12樓:漢秀梅夕羅

a="111"

b=int(a)

print

b,type(b)c=

str(b)

print

c,type(c)

如果解決了您的問題請採納!如果未解決請繼續追問

13樓:匿名使用者

整數字串轉換為對應的整數

int('12')

小數字串轉換為對應小數

float('12.34')

數字轉換為字串

str(123.45)

ascii碼轉換為相應字元

chr(97)

字元轉換為響應ascii碼

ord('a')

python怎麼實現字串和數字的轉換

14樓:匿名使用者

#py3

from functools import reducedigits=

def char2num(s): #單個字元轉數字return digits[s]

def str2int(s):#整數字串轉化整數,不支援浮點數.浮點數得另內寫一個

if s[0]=='-': #要判斷一下該數字有沒容有符號return -reduce(lambda x,y:10*x+y,map(char2num,s[1::])) #返回負數

else:

return reduce(lambda x,y:10*x+y,map(char2num,s)) #返回正數

a='-123'

print(100+str2int(a))

怎麼把字串轉換成16進位制資料,怎麼把一個字串轉換成16進位制資料

要看你的字串bai是怎du麼寫的。例如 zhi char s 07bc3a int v 你可以用 sscanf s,x v 變成整型值dao,存入 v.你也可以用 strtol 函式,專 v strtol s,null,16 賦給 v.列印屬16進位制數值 printf x v 將一個字串怎麼轉化成...

如何將字串轉換成十六進位制資料,怎麼把字串轉化為十六進位制字串java

字元型別是整型型別的一種,將其轉換成int型別,然後再輸出十六進位制形式即可。下面是一個示例程式。include using namespace std int main char str test hello world n int limit strlen str test for int in...

C 中怎樣把字串陣列中的數轉換成整形資料如 string s「1234」然後轉換成int n

可以直接來 用源atoi這個函 bai數 include iostream using namespace std int main 可以使用庫函式atoi。copy 一 函式名bai atoi 二 函式宣告 int atoi const char nptr 三 頭du檔案zhi c語言中用stdi...