Python 从字符串中删除初始单词
Python是一种常用的编程语言,用于许多不同的目的,例如Web开发、数据科学、机器学习以及执行许多不同的自动化处理。在本文中,我们将学习如何使用python从字符串中删除初始单词。
删除字符串中初始单词的不同方法
分割函数
在这个方法中,我们将把字符串拆分成子字符串,以删除初始字符串。我们将使用分割函数来实现相同的目的。使用分割函数从字符串中删除初始单词的语法如下:
def initial_string(string): #The string is given as input
data = string.split() #The string is then split
if len(data) > 1: #If it has more than one element then it is joined
return ' '.join(data[1:])
else:
return ""
示例
让我们举一个例子来理解它的用法:
def initial_string(string): #The string is given as input
data = string.split() #The string is then split
if len(data) > 1: #If it has more than one element then it is joined
return ' '.join(data[1:])
else:
return ""
whole_string = "Today, We will work on Python."
new_string = initial_string(whole_string)
print(new_string)
输出
上述示例的输出结果如下:
We will work on Python.
re模块
re模块提供正则表达式支持。它使用特定模式来查找特定的字符串或子字符串。我们将使用相同的方法来查找初始字符串,然后将其删除。使用re模块从字符串中删除初始单词的语法如下所示:
import re # Do not forget to import re module or else error might occur.
def initial_string(string): #The string is given as input
pattern = r'^\w+\s+' # A pattern is defined to find the initial string
return re.sub(pattern, '', string) # The initial string is substituted with an empty space
示例
上述方法的示例如下:
import re # Do not forget to import re module or else error might occur.
def initial_string(string): #The string is given as input
pattern = r'^\w+\s+' # A pattern is defined to find the initial string
return re.sub(pattern, '', string) # The initial string is substituted with an empty space
whole_string = "Today We will work on Python."
new_string = initial_string(whole_string)
print(new_string) #Displaying the output after removing the initial string
输出
上述方法的输出如下:
We will work on Python.
查找函数
find函数用于在字符串中根据位置查找特定的单词或元素。在这个方法中,我们将使用相同的函数来找到第一个位置的单词并将其删除。使用find函数删除初始单词的语法如下:
def initial_string(string): #The string is given as input
first_word = string.find(' ') #Find() is used to find the initial word of the string and if space is found it returns back
if first_word != -1:
return string[first_word + 1:]
else:
return ""
示例
使用上述语法的示例如下:
def initial_string(string): #The string is given as input
first_word = string.find(' ') #Find() is used to find the initial word of the string and if space is found it returns back
if first_word != -1:
return string[first_word + 1:]
else:
return ""
whole_string = "Today, We will work on Python."
new_string = initial_string(whole_string)
print(new_string) #Displaying the output after removing the initial string
输出
上述代码的输出结果如下:
We will work on Python.
空格分隔符
我们使用字符串分割函数将字符串分成两个不同的部分,然后移除具有多个元素的字符串,并移除初始单词。使用空格分隔符从字符串中移除初始单词的语法如下:
def initial_string(string): #The string is given as input
first = string.split(' ', 1) #Second argument to split the string into two parts
if len(first) > 1: # Checking for more than 1 element
return first[1] #If more than 1 element is present it returns the first part
else:
return "" #If only 1 element is present it returns the second part
示例
上述方法的示例如下:
def initial_string(string): #The string is given as input
first = string.split(' ', 1) #Second argument to split the string into two parts
if len(first) > 1: # Checking for more than 1 element
return first[1] #If more than 1 element is present it returns the first part
else:
return "" #If only 1 element is present it returns the second part
whole_string = "Today, We will work on Python."
new_string = initial_string(whole_string)
print(new_string) #Displaying the output after removing the initial string
输出
上面示例的输出结果如下:
We will work on Python.
分区方法
Python的分区函数根据选择的分隔符将字符串分成三部分。因此,将分隔符本身、分隔符之前的区域和分隔符之后的区域合并成一个元组。使用分区方法从字符串中删除初始单词的语法如下:
def initial_string(string): #The string is given as input
_, separator, remainder = string.partition(' ') # The partition method is used to seprate the string at the first space in the string
if separator: #If a seprator will be found then it will return remainder or else a empty space will return
return remainder
else:
return ""
示例
使用上述方法的示例如下:
def initial_string(string): #The string is given as input
_, separator, remainder = string.partition(' ') # The partition method is used to seprate the string at the first space in the string
if separator: #If a seprator will be found then it will return remainder or else a empty space will return
return remainder
else:
return ""
whole_string = "Today, We will work on Python."
new_string = initial_string(whole_string)
print(new_string) #Displaying the output after removing the initial string
输出
上述代码的输出结果如下:
We will work on Python.
结论
有很多不同的方法可以使用Python从字符串中去除初始单词,对于初学者来说,了解所有这些方法是必要的,这样他/她在不同领域的不同项目中可以在合适的问题上使用合适的逻辑。程序员可以选择自己喜欢的方法和适合编程领域的方法,以及可以减少编程时间的方法。