1

What I want to do is:

mystr = i_return_a_string()
fst_part, snd_part = mystr[:4], mystr[4:]

But I'd be happier if this can be done (with a built-in function/method) like:

fst_part, snd_part = i_return_a_string().cut_at(4)

This is somewhat like str.partition, the difference is that I want to specify the index.

  1. Is there a method for this?

  2. if not, is what I have above the best way?

In this example, mystr is only needed as a temporal storage for the return value (of the function i_return_a_string), never to be referred again, which makes me a little uncomfortable.

6
  • Have you looked through the documentation of str methods? Try writing your own function then if you have problrms post another question. Commented Sep 13, 2014 at 14:58
  • 5
    Your solution using slicing - fst_part, snd_part = mystr[:4], mystr[4:] - is the idiomatic way to do this in Python. There's no cut_at string method or function built in (but you could write one if you liked). Commented Sep 13, 2014 at 14:58
  • What's against slicing? If you want to skip the temporary mystr you could make i_return_a_string() return a tuple of strings - that is do the slicing already inside the function. Commented Sep 13, 2014 at 15:00
  • 2
    @Yosh: Python names are just references; no need to be anxious about using them. :-) Commented Sep 13, 2014 at 15:02
  • 1
    @Yosh, no, there's nothing wrong with using a temporary variable if you need one ... and it's certainly better than jumping through hoops to avoid one. Commented Sep 13, 2014 at 15:03

3 Answers 3

2

Summing up the comments given (as I don't dare leave this question open):

  • Using slicing is the idiomatic way (@ZeroPiraeus)
  • If I really don't like it, I could write my own function (@wwii, @ZeroPiraeus & @Bobby's answer) or I can change the base function so that it already returns a tuple(@Ghanima)
  • Using temporary variables are perfectly fine (@MartijnPieters, @ZeroPiraeus).
  • If I really, really, really needed to use built-in and blah, there's always another way (@myersjustinc 's answer).
Sign up to request clarification or add additional context in comments.

1 Comment

I intended this to be an answer, almost a complete one and certainly not just "additional information". These points listed above are basically what I needed to know. Q: Is there a method for this? -> A: No, but you can always write one. Q: Is what I have above the best way? -> A: Yes, and don't worry about mystr variable. I'm not getting why this is not considered to be an answer.
1

If you really felt the need to (although I think it makes more sense to use mystr[:4], mystr[4:] instead as in your example), you could use regular expressions:

fst_part, snd_part = re.match(r'(.{4})(.*)', i_return_a_string()).groups()

1 Comment

I agree it isn't a good solution, but it does what OP asked. It doesn't use an intermediate variable, it doesn't involve writing another function (because for whatever reason OP asked for a built-in), and it involves calling the i_return_a_string function only once.
0

Might not be the exact syntax you're looking for, but maybe this is something up your alley?

def i_return_a_string():
    return "examplestring"

def cut_at(example_string, index):
    return example_string[:index], example_string[index:]

fst_part, snd_part = cut_at(i_return_a_string(), 4)
print fst_part, snd_part

2 Comments

Yes, this is basically what I wanted. I wanted to know whether there's a default way to do that or not & whether I should write that function myself or use slicing.
There's nothing really built-in. I'd say write your own function if you're going to use that functionality a lot. Your original syntax reminds me of Ruby syntax where you can easily chain methods.

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.