ascii 函數將一個對象作為輸入,並以可打印格式返回該對象。該函數查找對象中的任何非 ascii 字符,並用相應的轉義字符替換該字符。
**ascii(object)** #where object can be a string, list, tuple etc
ascii()
參數:
ascii()
函數只有一個強制參數。它以字符串、元組、列表字典等對象為參數。
參數 | 描述 | 必需/可選 |
---|---|---|
目標 | 任何對象,如字符串、列表、字典等。 | 需要 |
ascii()
返回值
如果對象中存在非 ascii 字符,它將被轉換為可打印格式。
Python 中ascii()
方法的示例
示例 1:傳遞帶有非 ascii 字符的字符串
obj = "I löve lèarning Pythön" print(obj)
print(ascii(obj))
輸出:
I löve lèarning Pythön
'I l\xf6ve l\xe8arning Pyth\xf6n'
示例 2:傳遞僅包含 ascii 字符的字符串
obj = "I love learning Python" print(obj)
print(ascii(obj))
輸出:
I love learning Python
'I love learning Python'
示例 3:傳遞一個包含 ascii 和非 ascii 字符串以及一個整數的 iterable
tup_obj = ('L3arn', 'Lèarn', 3) print(ascii(tup_obj))
輸出:
('L3arn', 'L\xe8arn', 3)
原創文章,作者:PTPUU,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/127832.html