Rumah > Artikel > pembangunan bahagian belakang > Bagaimana Mengekstrak Aksara Mengikuti Pembatas dalam String menggunakan Python?
Finding Characters Following a Delimiter in a String using Python
In this scenario, you have a string with a consistent pattern where it begins with a set of numbers followed by an underscore. Your goal is to extract the remaining characters after this underscore.
To achieve this, you can utilize the substr and strpos functions in Python:
data = "123_String" index = data.find("_") + 1 # Find the index of the underscore character result = data[index:] # Extract the characters after the index print(result) # Output: "String"
In the above example, the find function returns the index of the underscore character, and the substr function then extracts the characters from that index onwards, effectively retrieving "String."
Alternatively, you can use a conditional statement to check for the existence of the underscore character before extracting the desired characters:
if "_" in data: index = data.find("_") + 1 result = data[index:] print(result)
This ensures that the extraction only occurs if the underscore character is present in the string, avoiding potential errors.
Atas ialah kandungan terperinci Bagaimana Mengekstrak Aksara Mengikuti Pembatas dalam String menggunakan Python?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!