如何將隨機(jī)數(shù)數(shù)轉(zhuǎn)換為漢字python
- 教育綜合
- 2024-06-15 07:57:22
Python數(shù)字轉(zhuǎn)漢字?
你好,代碼如下:
--------------------
simplify = {'1':'壹', '2':'貳', '3':'叁','4':'肆', '5':'伍','6':'陸', '7':'染', '8':'捌', '9':'玖', '10':'拾
' }
num_string = input('請(qǐng)輸入任意數(shù)字: ')
print(''.join([simplify[i] for i in num_string]))
--------------------
執(zhí)行結(jié)果見(jiàn)視頻:
python中將阿拉伯?dāng)?shù)字轉(zhuǎn)換為中文
第一種方案: def num_to_char(num): """數(shù)字轉(zhuǎn)中文""" num=str(num) new_str="" num_dict={"0":u"零","1":u"一","2":u"二","3":u"三","4":u"四","5":u"五","6":u"六","7":u"七","8":u"八","9":u"九"} listnum=list(num) # print(listnum) shu=[] for i in listnum: # print(num_dict[i]) shu.append(num_dict[i]) new_str="".join(shu) # print(python 怎樣隨機(jī)生成中文字符
用Python隨機(jī)生成字符串: from random import Random def random_str(randomlength=8): //固定長(zhǎng)度8 str = '' //str初始為空 chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789' length = len(chars) - 1 random = Random() //random模塊用于生成隨機(jī)數(shù) for i in range(randomlength): //循環(huán)生成隨機(jī)字符串 str+=chars[random.randin在Python里 16進(jìn)制的字符串怎么轉(zhuǎn)化為漢字呢
做下進(jìn)制轉(zhuǎn)換就行了 #python 2.6以后內(nèi)置函數(shù) #10進(jìn)制轉(zhuǎn)為2進(jìn)制 >>> bin(10) '0b1010' #2進(jìn)制轉(zhuǎn)為10進(jìn)制 >>> int("1001",2) 9 #10進(jìn)制轉(zhuǎn)為16進(jìn)制 >>> hex(10) '0xa' #16進(jìn)制到10進(jìn)制 >>> int('ff', 16) 255 >>> int('0xab', 16) 171 #十進(jìn)制轉(zhuǎn)為八進(jìn)制 >>print("%o" % 10) >>12 #16進(jìn)制到2進(jìn)制 >>> bin(0xa) '0b1010' >>>python 如何將亂碼轉(zhuǎn)成漢字
1、python2與python3稍微有點(diǎn)區(qū)別 2、python2中默認(rèn)的字符編碼格式都是unicode,在字符串前加'u',表示unicode 編碼 3、將unicode轉(zhuǎn)換成中文,只需要用deconde解碼就可以了 >>> u='歡迎'>>> e=u.encode()>>> eb'\xe6\xac\xa2\xe8\xbf\x8e'>>> e.decode()#python3中默認(rèn)就是utf-8編碼'歡迎'>>> e.decode('gbk')#如果解碼為gbk就是亂碼'嬈四繋' python2編碼環(huán)境比較復(fù)雜,在這里不做詳細(xì)說(shuō)明展開(kāi)全文閱讀