5

I've got the following string: blah blah blah blah in Rostock

What's the pythonic way for removing all the string content from the word 'in' until the end, leaving the string like this: 'blah blah blah blah'

9
  • 3
    What have you tried that you consider un-pythonic? Commented Jan 30, 2018 at 18:26
  • Have you tried using regex? Commented Jan 30, 2018 at 18:26
  • 1
    And perhaps you should not use people's real names to protect their privacy Commented Jan 30, 2018 at 18:27
  • 1
    good idea @ThomasWeller Commented Jan 30, 2018 at 18:29
  • 2
    @IanSpitz: please learn to write down all your requirements. As you can see, the question is unclear: How many of those strings do you have? Can "in" occur only in the end or are there other names like "Konstantin" that have "in" in their name. Do you want spaces to be stripped or not? Can there be other cities containing "in" like "Berlin"? IMHO, For a single name, it's not worth writing a program at all. Commented Jan 30, 2018 at 18:38

4 Answers 4

6

Using split(" in "), you can split the string from the "in".

This produces a list with the two ends. Now take the first part by using [0]:

string.split(" in ")[0]

If you don't want the space character at the end, then use rstrip(): string.split(" in ")[0].rstip()

Welcome.

Sign up to request clarification or add additional context in comments.

12 Comments

What if there's a in in the name like "Justin"?
@ThomasWeller, that was not part of the OP's requirements
Bear in mind cases like 'blah blah blinah in Rostock.' if you take this approach. At the very least, use split(" in ") rather than split("in").
@ChrisLarson but I used the rstrip to remove spaces. But true point if the "in" is in the name. Lets hope not.
If OP would want to remove that part only on exactly that data, why would he write a program at all? He could just remove the unwanted part from the original data
|
1

Use regular expression if the base unit is word.

import re
line = 'justin in Rostock'
print(re.split(r'\bin\b', line, maxsplit=1)[0].strip())

justin

example in regular expression

Use str.partition if the base unit is character.

line = 'blah blah blah blah in Rostock'
new_string = line.partition('in')[0].strip()

print(new_string)

blah blah blah blah

strip() removes the space before in

3 Comments

Why split at the first occurrence and not the last?
line = 'justin in Rostock' will get partitioned as just
regex is used to split by words
1

I don't know whether you call it pythonic or not. At least it seems to do the job.

def getNameAndCity(nameWithCity: str) -> (str, str):
    if not " in " in nameWithCity:
        return nameWithCity, None
    pieces = nameWithCity.split(" in ")
    name = " in ".join(pieces[0:-1])
    return name, pieces[-1]

# No 'in' at all
assert ("Michael",None) == getNameAndCity("Michael")
# Nothing special
assert ("Johan", "Oslo") == getNameAndCity("Johan in Oslo")
# "'in' in City
assert ("Sandra", "Berlin") == getNameAndCity("Sandra in Berlin")
# 'in' in Name and City
assert ("Christine", "Berlin") == getNameAndCity("Christine in Berlin")
# 'in' as an extra token
assert ("Christine in Love", "Berlin") == getNameAndCity("Christine in Love in Berlin")

Comments

0
s = "Ahe, Christiane Dr. von der Praxis für Kieferorthopädie in Rostock"
if " in " in s:
    s = s[:s.find(" in ")]
# Else leave the string untouched.

5 Comments

Almost. This will skip the last character in the string if the thing you are finding does not exist
@smac89 Why would you make such a special case?
Because special cases are the only ones that matter
index = s.find('in'); s = s[:index] if index > 0 else s
if it's harder to read, then don't use it. I was just suggesting a different approach

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.