For getting only first word use index after splitting.
a="abcde!mdamdskm"
print a.split("!")[0]
Answer from Rohit-Pandey on Stack Overflow Top answer 1 of 2
19
For getting only first word use index after splitting.
a="abcde!mdamdskm"
print a.split("!")[0]
2 of 2
8
>>> "abcde!mdamdskm".split("!")
['abcde', 'mdamdskm']
This might not work if you have multiple instances of the special character:
>>> "abcde!mdam!dskm".split("!")
['abcde', 'mdam', 'dskm']
But you could fix that like this:
>>> "abcde!mdam!dskm".split("!", 1)
['abcde', 'mdam!dskm']
GeeksforGeeks
geeksforgeeks.org โบ python โบ string-slicing-in-python
String Slicing in Python - GeeksforGeeks
s[-8:-1:2] slices the string from the 8th character from the end ('h') to the 2nd character from the end ('n'), taking every second character. To reverse a string, use a negative step value of -1, which moves from the end of the string to the ...
Published: October 7, 2026
06:47
๐ Python Tutorial #26: String Slicing - YouTube
13:15
String Slicing | Python Tutorial - YouTube
00:59
How To Slice A String In Python - YouTube
String Slicing in Python - Master [start:end:step] Syntax Complete ...
16:57
String Slicing in Python (Part 1) - YouTube
How to Slice a String in Python #programming #code #python
Real Python
realpython.com โบ python-strings
Strings and Character Data in Python โ Real Python
October 5, 2026 - If the first index in a slicing is greater than or equal to the second index, then Python returns an empty string: ... As you can see, this behavior provides a somewhat obfuscated way to generate empty strings. You can also use negative indices in slicing. Just as with indexing, -1 refers to the last character, -2 to the second-last character, and so on. The diagram below shows how to slice the substring "oob" from the string "foobar" using both positive and negative indices:
W3Schools
w3schools.com โบ python โบ gloss_python_string_slice.asp
Python Slice Strings
You can return a range of characters by using the slice syntax. Specify the start index and the end index, separated by a colon, to return a part of the string. Get the characters from position 2 to position 5 (not included): b = "Hello, World!" print(b[2:5]) Try it Yourself ยป ยท Python Strings ...
Linode
linode.com โบ docs โบ guides โบ how-to-slice-and-index-strings-in-python
How to Slice and Index Strings in Python | Linode Docs
January 28, 2022 - It specifies how Python should walk through the string while generating the substring. A stride of 2 tells Python to select every second character. A stride of -2, however, indicates that every second letter should be selected from right to left. The stride is specified as an optional third argument to the slice operation using the format string[starting_position:ending_postion:stride]. The default stride is 1, which results in the behavior seen in the previous slice operations.
Python Central
pythoncentral.io โบ cutting-and-slicing-strings-in-python
Cutting and slicing strings in Python - Python Central
September 6, 2023 - In other words, we can tell Python to look for a certain substring within our target string, and split the target string up around that sub-string. It does that by returning a list of the resulting sub-strings (minus the delimiters). By the way, we can choose not to specify a delimiter explicitly, in which case it defaults to a white-space character (space, '\t', '\n', '\r', '\f') or sequence of such characters.
Manifoldapp
cuny.manifoldapp.org โบ read โบ how-to-code-in-python-3 โบ section โบ 93337ae8-329e-493b-b15a-7fe26f12f037
How To Index and Slice Strings | How To Code in Python 3 | Manifold @CUNY
With slices, we can call multiple character values by creating a range of index numbers separated by a colon [x:y]: ... When constructing a slice, as in [6:11], the first index number is where the slice starts (inclusive), and the second index number is where the slice ends (exclusive), which is why in our example above the range has to be the index number that would occur just after the string ends.
Top answer 1 of 5
20
Use itertools.groupby:
from itertools import groupby
s = 'abccbba'
print([''.join(v) for _, v in groupby(s)])
# ['a', 'b', 'cc', 'bb', 'a']
2 of 5
8
It can be achieved with re.finditer():
import re
s = 'aabccdd'
print([m.group(0) for m in re.finditer(r"(\w)\1*", s)])
#['aa', 'b', 'cc', 'dd']
W3Schools
w3schools.com โบ python โบ ref_string_split.asp
Python String split() Method
abs() all() any() ascii() bin() bool() bytearray() bytes() callable() chr() compile() complex() delattr() dict() dir() divmod() enumerate() eval() exec() filter() float() format() frozenset() getattr() globals() hasattr() hex() id() input() int() isinstance() issubclass() iter() len() list() locals() map() max() memoryview() min() next() object() oct() open() ord() pow() print() range() reversed() round() set() setattr() slice() sorted() str() sum() super() tuple() type() vars() zip() Python String Methods
PythonForBeginners.com
pythonforbeginners.com โบ home โบ how to split a string between characters in python
How to Split a String Between Characters in Python - PythonForBeginners.com
August 17, 2021 - If you do meet Horatio and Marcellus, The rivals of my watch, bid them make haste.""" speaker = text[:8] print(speaker) ... To use this method, we need to know the start and end location of the substring we want to slice. We can use the index() method to find the index of a character in a string.
freeCodeCamp
freecodecamp.org โบ news โบ python-substring-how-to-slice-a-string
Python Substring โ How to Slice a String
July 27, 2021 - In this example, you will slice the last 4 characters from the string. Here you use the negative index to start slicing from the end of the string. ... The output will be โcampโ. You can slice the string with steps after indicating a start-index and stop-index. By default, the step is 1 but in the following example, the step size is 2.
InterServer
interserver.net โบ home โบ python โบ how to slice strings in python: step-by-step tutorial
How to Slice Strings in Python: Step-by-Step Tutorial - Interserver Tips
April 23, 2026 - The step parameter lets you skip characters in a string. It is written as the third value in the slice: [start:stop:step]. For example, if text = "PythonProgramming", you can get every second character using:
Tutorialspoint
tutorialspoint.com โบ python โบ python_slicing_strings.htm
Python Slicing Strings
In this process, we extract a portion or piece of a string. Usually, we use the slice operator "[ : ]" to perform slicing on a Python String. Before proceeding with string slicing let's understand string indexing.