Extracting only characters from a string in Python

前端 未结 7 1165
后悔当初
后悔当初 2020-12-08 07:01

In Python, I want to extract only the characters from a string.

Consider I have the following string,

input = \"{(\'players\',): 24, (\'year\',): 28         


        
7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-08 08:01

    You can take the approach of iterating through the string, and using the isalpha function to determine if it's a alpha character or not. If it is you can append it to the output string.

    a = "Some57 996S/tr::--!!ing"
    q = ""
    for i in a:
        if i.isalpha():
            q = "".join([q,i])
    

提交回复
热议问题