且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将空格分隔的字符串转换为列表

更新时间:2023-02-23 10:00:19

states.split() 将返回

['Alaska',
 'Alabama',
 'Arkansas',
 'American',
 'Samoa',
 'Arizona',
 'California',
 'Colorado']

如果你需要一个随机的,那么你必须使用 random 模块:

If you need one random from them, then you have to use the random module:

import random

states = "... ..."

random_state = random.choice(states.split())