📜  Python|仅从给定字符串中提取字符

📅  最后修改于: 2022-05-13 01:54:53.458000             🧑  作者: Mango

Python|仅从给定字符串中提取字符

给定一个字符串,任务是只从字符串中提取字母字符。下面给出了解决给定问题的几种方法。

方法 #1:使用re.split

# Python code to demonstrate
# to get characters from string
import re
  
# initialising string
ini_string = "123()#$ABGFDabcjw"
ini_string2 = "abceddfgh"
  
# printing strings
print ("initial string : ", ini_string, ini_string2)
  
# code to find characters in string
res1 = " ".join(re.split("[^a-zA-Z]*", ini_string))
res2 = " ".join(re.split("[^a-zA-Z]*", ini_string2))
  
# printing resultant string
print ("first string result: ", str(res1))
print ("second string result: ", str(res2))

输出:

initial string :  123()#$ABGFDabcjw abceddfgh
first string result:   ABGFDabcjw
second string result:  abceddfgh


方法 #2:使用re.fidall

# Python code to demonstrate
# to get characters in string
import re
  
# initialising string
ini_string = "123()#$ABGFDabcjw"
ini_string2 = "abceddfgh"
  
# printing strings
print ("initial string : \n", ini_string, "\n", ini_string2)
  
# code to find characters in string
res1 = " ".join(re.findall("[a-zA-Z]+", ini_string))
res2 = " ".join(re.findall("[a-zA-Z]+", ini_string2))
  
# printing resultant string
print ("first string result: ", str(res1))
print ("second string result: ", str(res2))

输出:

initial string : 
 123()#$ABGFDabcjw 
 abceddfgh

first string result:  ABGFDabcjw
second string result:  abceddfgh


方法 #3:使用isalpha()

# Python code to demonstrate
# to get characters in a string
# if present
  
# initialising string
ini_string = "123()#$ABGFDabcjw"
  
# printing string and its length
print ("initial string : ", ini_string)
  
# code to find characters in string
res1 = ""
for i in ini_string:
    if i.isalpha():
        res1 = "".join([res1, i])
  
  
# printing resultant string
print ("first result: ", str(res1))

输出:

initial string :  123()#$ABGFDabcjw
first result:  ABGFDabcjw