About 78,100 results
Open links in new tab
  1. slice - How slicing in Python works - Stack Overflow

    It will return an empty string. (ii) If the step size if a negative value, upper bound should be lesser than lower bound, otherwise empty string will be printed. For example: s="Welcome" s1=s[3:0:-1] print(s1) The output: cle But if we run the following code: s="Welcome" s1=s[0:5:-1] print(s1) The output will be an empty string. Thus in the code:

  2. python - Ways to slice a string? - Stack Overflow

    Jul 31, 2018 · The s[:-3] gives you a string up to, but not including, the comma you want removed ("this is a string") and the s[-2:] gives you another string starting one character beyond that comma (" a"). Then, joining the two strings together gives you …

  3. Slice to the end of a string without using len () - Stack Overflow

    You could always just do it like this if you want to only omit the first character of your string: word[1:] Here you are specifying that you want the characters from index 1, which is the second character of your string, till the last index at the end. This means you only slice the character at the first index of the string, in this case 'H'.

  4. Python - How to cut a string in Python? - Stack Overflow

    Nov 23, 2011 · Like, if you always wanted to remove the two argument, even if it was put earlier in the query string ("two=20&s=some"), this would still do the right thing. It might be overkill depending on what you want to do.

  5. Split a string by a delimiter in Python - Stack Overflow

    When you want to split a string by a specific delimiter like: __ or | or , etc. it's much easier and faster to split using .split() method as in the top answer because Python string methods are intuitive and optimized. However, if you need to split a string using a pattern (e.g. " __ "and "__"), then using the built-in re module might be useful.

  6. How do I get a substring of a string in Python? - Stack Overflow

    Aug 31, 2016 · @gimel: Actually, [:] on an immutable type doesn't make a copy at all. While mysequence[:] is mostly harmless when mysequence is an immutable type like str, tuple, bytes (Py3) or unicode (Py2), a = b[:] is equivalent to a = b, it just wastes a little time dispatching the slicing byte codes which the object responds to by returning itself since it's pointless to shallow …

  7. How would I get everything before a : in a string Python

    Feb 14, 2019 · I have benchmarked these various technics under Python 3.9.13 (Jupyter Notebook). TLDR. When I know that c is in s, I would use partition for small strings and index for long strings.

  8. python - Slice string by character and not index - Stack Overflow

    I want to create variable from a long string to a small string. e.g abcde!mdamdskm to abcde. I know only what is the special character and not the index.

  9. python - How to get a string after a specific substring ... - Stack ...

    Jul 4, 2021 · Note that this produces an empty string if the delimiter is missing: >>> my_string.partition("Monty")[2] # delimiter missing '' If you want to have the original string, then test if the second value returned from str.partition() is non-empty: prefix, success, result = my_string.partition(delimiter) if not success: result = prefix

  10. Python String Slicing Stride Clarification - Stack Overflow

    Oct 14, 2011 · This is because you didn't provide from which position the slice would be executed. So it'll go back by 2 characters from the last one until it reaches the first one. "123456"[1::-2] Produces "2" because you told Python to start from the 2nd character (index 1 from the string) and you told Python to go back 2 steps from that position.

Refresh