import re
state_names=['Alabama','Alaska','Arizona','Arkansas','California','Colorado','Connecticut','Delaware','Florida','Georgia','Hawaii','Idaho','Illinois','Indiana','Iowa','Kansas','Kentucky','Louisiana','Maine','Maryland','Massachusetts','Michigan','Minnesota','Mississippi','Missouri','Montana','Nebraska','Nevada','New Hampshire','NewJersey','NewMexico','NewYork','NorthCarolina','NorthDakota','Ohio','Oklahoma','Oregon','Pennsylvania','RhodeIsland','SouthCarolina','SouthDakota','Tennessee','Texas','Utah','Vermont','Virginia','Washington','WestVirginia','Wisconsin','Wyoming']
a = str(state_names)
b=re.findall('^M', a)
print(b)
I want to replace state_names with a string and print out all the characters that start with M, but if I write it like this, only [] is output. Did something go wrong?
regex
b=re.findall('\'M\w+\'', a)
Switch to and try. I think you can study more about regular expressions.
© 2024 OneMinuteCode. All rights reserved.