Wednesday, August 23, 2023

python substring output "jkl-mno-test" from string "abc:def:ghi::jkl-mno-test"

python substring output "jkl-mno-test" from string "abc:def:ghi::jkl-mno-test" To extract the substring "jkl-mno-test" from the string "abc:def:ghi::jkl-mno-test" in Python, you can use slicing. Here's how you can do it:
original_string = "abc:def:ghi::jkl-mno-test"
substring = original_string.split("::")[-1]
print(substring)
In this code, split("::") breaks the original string into parts using "::" as the delimiter and returns a list of parts. Since you're interested in the last part, you can use [-1] to access the last element of the list, which will be "jkl-mno-test". The output will be:
jkl-mno-test

No comments:

Post a Comment