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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

在Python中将字符串拆分为字符数组

發(fā)布時間:2023/12/1 python 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在Python中将字符串拆分为字符数组 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Given a string and we have to split into array of characters in Python.

給定一個字符串,我們必須在Python中拆分為字符數(shù)組。

將字符串拆分為字符 (Splitting string to characters)

1) Split string using for loop

1)使用for循環(huán)分割字符串

Use for loop to convert each character into the list and returns the list/array of the characters.

使用for循環(huán)將每個字符轉(zhuǎn)換為列表并返回字符的列表/數(shù)組。

Python program to split string into array of characters using for loop

Python程序使用for循環(huán)將字符串拆分為字符數(shù)組

# Split string using for loop# function to split string def split_str(s):return [ch for ch in s]# main code string = "Hello world!"print("string: ", string) print("split string...") print(split_str(string))

Output

輸出量

string: Hello world! split string... ['H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!']

2) Split string by converting string to the list (using typecast)

2)通過將字符串轉(zhuǎn)換為列表來分割字符串(使用類型轉(zhuǎn)換)

We can typecast string to the list using list(string) – it will return a list/array of characters.

我們可以使用list(string)將字符串類型轉(zhuǎn)換到列表中-它會返回一個字符列表/數(shù)組。

Python program to split string into array by typecasting string to list

Python程序通過將字符串類型轉(zhuǎn)換為列表將字符串拆分為數(shù)組

# Split string by typecasting # from string to list# function to split string def split_str(s):return list(s)# main code string = "Hello world!"print("string: ", string) print("split string...") print(split_str(string)) string: Hello world! split string... ['H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!']

翻譯自: https://www.includehelp.com/python/split-a-string-into-array-of-characters.aspx

總結(jié)

以上是生活随笔為你收集整理的在Python中将字符串拆分为字符数组的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。