-2

i have a input and i want to split the text to get the desired output i have used txt.split(":")[-1] but it's not solving my issue.Need help new to python coding.

input:End Date: 10-09-2020 10:00 AM
Desired output: 10-09-2020 10:00 AM

Deepak Jain
  • 137
  • 1
  • 3
  • 27
  • If you want to go with your approach, you probably need to use strip() and .join() to get to your desired output – Saskia Sep 10 '20 at 07:14
  • did you `print (txt.split(":"))` - why do you use `[-1]` ? what can you change in your code to get what you want? did you read [the docs](https://docs.python.org/3/library/stdtypes.html#str.split)? – Patrick Artner Sep 10 '20 at 07:15
  • Duplicate: [splitting-on-first-occurrence](https://stackoverflow.com/questions/6903557/splitting-on-first-occurrence) – Patrick Artner Sep 10 '20 at 07:17

2 Answers2

2

try:

input = "End Date: 10-09-2020 10:00 AM"
matcher = "End Date: " # get a constant identifier to look for

# now we want to slice the string in such way that leaves only the part 
# after the matcher. So we find the first occurence of matcher and then
# add it's length to see where is the end of the matcher. then we
# simply take the remaining string
desired = input[input.find(matcher)+len(matcher):]
snatchysquid
  • 1,283
  • 9
  • 24
1
txt.split("End Date:")[-1]

it will solve the issue