日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python整数转换字符串_Python | 将字符串转换为整数列表

發布時間:2025/3/11 python 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python整数转换字符串_Python | 将字符串转换为整数列表 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

python整數轉換字符串

Given a string with digits and we have to convert the string to its equivalent list of the integers in Python.

給定一個帶有數字的字符串,我們必須將該字符串轉換為Python中與之等效的整數列表。

Example:

例:

Input:str1 = "12345"Output:int_list = [1, 2, 3, 4, 5]Input:str1 = "12345ABCD"Output:ValueError

Note: The string must contain only digits between 0 to 9, if there is any character except digits, the program will through a ValueError.

注意:該字符串只能包含0到9之間的數字,如果除數字之外還有任何其他字符,則程序將通過ValueError進行操作 。

如何將字符(僅數字)轉換為整數? (How to convert character (only digit) to integer?)

To convert a character (that is digit only like: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9') to integer, we use int() function - which is a library function in Python. int() returns integer value equivalent to given character i.e. digit in character form.

轉換字符(僅是數字,例如: “ 0”,“ 1”,“ 2”,“ 3”,“ 4”,“ 5”,“ 6”,“ 7”,“ 8”,“ 9” )轉換為整數,我們使用int()函數-這是Python中的庫函數。 int()返回等于給定字符的整數值,即字符形式的數字。

print (int('0')) print (int('1')) print (int('2')) print (int('3')) print (int('4')) print (int('5')) print (int('6')) print (int('7')) print (int('8')) print (int('9'))

Output

輸出量

0 1 2 3 4 5 6 7 8 9

Python程序將字符串轉換為整數列表 (Python program to convert a string to integers list)

Here, we have a string "12345" and we are converting it to integers list [1, 2, 3, 4, 5].

在這里,我們有一個字符串“ 12345” ,并將其轉換為整數列表[1、2、3、4、5] 。

# program to convert string to integer list # language: python3# declare a list str1 = "12345"# list variable to integeres int_list =[]# converting characters to integers for ch in str1:int_list.append(int(ch))# printing the str_list and int_list print ("str1: ", str1) print ("int_list: ", int_list)

Output

輸出量

str1: 12345 int_list: [1, 2, 3, 4, 5]

ValueError (ValueError)

If there is any character except the digits, there will be an error "ValueError: invalid literal for int() with base 10".

如果除數字外沒有其他字符,將出現錯誤“ ValueError:int()的基數為10的無效文字” 。

Program with Error:

出現錯誤的程序:

# program to convert string to integer list # language: python3# declare a list str1 = "12345ABCD"# list variable to integeres int_list =[]# converting characters to integers for ch in str1:int_list.append(int(ch))# printing the str_list and int_list print ("str1: ", str1) print ("int_list: ", int_list)

Output

輸出量

Traceback (most recent call last):File "/home/main.py", line 12, in int_list.append(int(ch)) ValueError: invalid literal for int() with base 10: 'A'

翻譯自: https://www.includehelp.com/python/convert-a-string-to-integers-list.aspx

python整數轉換字符串

總結

以上是生活随笔為你收集整理的python整数转换字符串_Python | 将字符串转换为整数列表的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。