How do you split a string at a specific position in python?

How can I split a string by the position of a word?

My data looks like this:

test = 'annamarypeterson, Guest Relations Manager, responded to this reviewResponded 1 week agoDear LoreLoreLore,Greetings from Amsterdam!We have received your wonderful comments and wanted to thank you for sharing your positive experience with us. We are so thankful that you have selected the Andaz Amsterdam for your special all-girls weekend getaway. Please come and see us again in the near future and let our team pamper you and your girlfriends!!Thanks again!Anna MaryAndaz Amsterdam -Guest RelationsReport response as inappropriateThank you. We appreciate your input.This response is the subjective opinion of the management representative'

I need this output:

responder = 'annamarypeterson, Guest relations Manager'
date = 'Responded 1 week ago'
response = 'Dear ....' #without 'This response is the subjective opinion of the management representative'

I know that the find.() function gives the position of a word, and I want to use this position to tell Python where to split it. For example:

splitat = test.find('ago')+3

What function can I use to split with an integer? The split() function does not work with an int.

asked Oct 16, 2017 at 9:01

How do you split a string at a specific position in python?

4

You can do this with strings (and lists) using slicing:

string = "hello world!"
splitat = 4
left, right = string[:splitat], string[splitat:]

will result in:

>>> left
hell
>>> right
o world!

answered Oct 16, 2017 at 9:05

JurgyJurgy

1,94519 silver badges29 bronze badges

Maybe the simplest solution is to use string slicing:

test = 'annamarypeterson, Guest Relations Manager, responded to this reviewResponded 1 week agoDear LoreLoreLore,Greetings from Amsterdam!We have received your wonderful comments and wanted to thank you for sharing your positive experience with us. We are so thankful that you have selected the Andaz Amsterdam for your special all-girls weekend getaway. Please come and see us again in the near future and let our team pamper you and your girlfriends!!Thanks again!Anna MaryAndaz Amsterdam -Guest RelationsReport response as inappropriateThank you. We appreciate your input.This response is the subjective opinion of the management representative'
pos = test.find('ago') + 3
print(test[:pos], test[pos:])

answered Oct 16, 2017 at 9:06

How do you split a string at a specific position in python?

Oleh RybalchenkoOleh Rybalchenko

5,7783 gold badges20 silver badges35 bronze badges

Got a built in function for you.

import re    
test = 'annamarypeterson, Guest Relations Manager, responded to this reviewResponded 1 week agoDear LoreLoreLore,Greetings from Amsterdam!We have received your wonderful comments and wanted to thank you for sharing your positive experience with us. We are so thankful that you have selected the Andaz Amsterdam for your special all-girls weekend getaway. Please come and see us again in the near future and let our team pamper you and your girlfriends!!Thanks again!Anna MaryAndaz Amsterdam -Guest RelationsReport response as inappropriateThank you. We appreciate your input.This response is the subjective opinion of the management representative'

m = re.search('ago', test)
response = test[m.end():]

This is the object produced by regex:

You can use m.start() to get the position of the first character and m.end() to get the last character.

https://docs.python.org/3/library/re.html

answered Nov 16, 2021 at 12:34

How do you split a string at a specific position in python?

Python String split() method in Python split a string into a list of strings after breaking the given string by the specified separator.

Python String split() Method Syntax

Syntax : str.split(separator, maxsplit)

Parameters :

  • separator : This is a delimiter. The string splits at this specified separator. If is not provided then any white space is a separator.
  • maxsplit : It is a number, which tells us to split the string into maximum of provided number of times. If it is not provided then the default is -1 that means there is no limit.

Returns : Returns a list of strings after breaking the given string by the specified separator.

Python String split() Method Example

Python3

string = "one,two,three"

words = string.split(',')

print(words)

Output:

['one', 'two', 'three']

Example 1: Example to demonstrate how split() function works

Here we are using the Python String split() function to split different Strings into a list, separated by different characters in each case.

Python3

text = 'geeks for geeks'

print(text.split())

word = 'geeks, for, geeks'

print(word.split(','))

word = 'geeks:for:geeks'

print(word.split(':'))

word = 'CatBatSatFatOr'

print(word.split('t'))

Output :

['geeks', 'for', 'geeks']
['geeks', ' for', ' geeks']
['geeks', 'for', 'geeks']
['Ca', 'Ba', 'Sa', 'Fa', 'Or']

Example 2: Example to demonstrate how split() function works when maxsplit is specified

The maxsplit parameter is used to control how many splits to return after the string is parsed. Even if there are multiple splits possible, it’ll only do maximum that number of splits as defined by maxsplit parameter.

Python3

word = 'geeks, for, geeks, pawan'

print(word.split(', ', 0))

print(word.split(', ', 4))

print(word.split(', ', 1))

Output :

['geeks, for, geeks, pawan']
['geeks', 'for', 'geeks', 'pawan']
['geeks', 'for, geeks, pawan']

How do you split a string by position in Python?

Python String split() Method Syntax separator : This is a delimiter. The string splits at this specified separator. If is not provided then any white space is a separator. maxsplit : It is a number, which tells us to split the string into maximum of provided number of times.

How do I split a string in a specific position?

To split a string at a specific index, use the slice method to get the two parts of the string, e.g. str. slice(0, index) returns the part of the string up to, but not including the provided index, and str. slice(index) returns the remainder of the string.

How do you split a string after a specific character in Python?

The split() function returns a list of objects containing elements. Any whitespace character, such as space, t, n, and so on, is used as the default separator. Separator and maxsplit are the two arguments for this function. The delimiter string (optional) is the separator string.