getting first word after a special character in a string in python

  • Last Update :
  • Techknowledgy :

In this tutorial, we will be solving a program of python get first word in string. We will be discussing the solution to get first word in string in python in 3 ways.,Above we have solved python get first word in string in 3 ways. We can get first word in string in python by using string split() method, re.split() and by using for loop., Python get first word in string (3 Ways),The easiest way to get the first word in string in python is to access the first element of the list which is returned by the string split() method.

Syntax:-

String.split(seperator)

Python code:

# define the string
my_string = "Python Java Ruby JavaScript"
# Using split() method
first_word = my_string.split()[0]
# Printing
print("First word in my string is", first_word)

Output:

First word in my string is Python

Syntax –

re.split(pattern, string)

Python Code-

#
import regex module
import re
# define the string
my_string = "Python Java Ruby JavaScript"
# Using regex split()
first_word = re.split("s", my_string)[0]
# Printing
print("First word in my string is", first_word)

Suggestion : 2

Here is your code with tests

import re

s = '@VirginAmerica it was amazing, and arrived an hour early.'
t = 'heyyyyy@VirginAmerica , am I dreaming?'
m = 'heyyyyy @VirginAmerica , am I dreaming?'
u = ''
f = '@United...'
h = '@United@VirginAmerica'

def find_match(str):
   res = re.search('@(\w+)', str)
if not res:
   return ''
return res.group(1)

def sub_match(str):
   return re.sub('^[^@]*@\w+', '', str)

assert find_match(s) == 'VirginAmerica'
assert find_match(t) == 'VirginAmerica'
assert find_match(m) == 'VirginAmerica'
assert find_match(u) == ''
assert find_match(f) == 'United'
assert find_match(h) == 'United'

assert sub_match(s) == ' it was amazing, and arrived an hour early.'
assert sub_match(t) == ' , am I dreaming?'
assert sub_match(m) == ' , am I dreaming?'
assert sub_match(u) == ''
assert sub_match(f) == '...'
assert sub_match(h) == '@VirginAmerica'

Another implementation using regex, this gets the word following @ from the string.

import re

s = '@VirginAmerica it was amazing, and arrived an hour early.'
t = 'heyyyyy@VirginAmerica , am I dreaming?'
m = 'heyyyyy @VirginAmerica , am I dreaming?'
u = ''
f = '@United...'
h = '@United@VirginAmerica'

for text in [s, t, m, u, f, h]:
   print(re.findall(r '@(\w+)', text))

this prints

['VirginAmerica ']
['VirginAmerica ']
['VirginAmerica ']
[]
['United']
['United', 'VirginAmerica']

If you don't want to use regex you can use split still but something like this will result in the same as the above:

s = '@VirginAmerica it was amazing, and arrived an hour early.'
t = 'heyyyyy@VirginAmerica , am I dreaming?'
m = 'heyyyyy @VirginAmerica , am I dreaming?'
u = ''
f = '@United...'
h = '@United@VirginAmerica'

for text in [s, t, m, u, f, h]:
   _, * words = text.split('@')
print([words.split()[0]
   for word in words
])

To get the word without the first occurence of the @word use sub I also included a space and question mark after it to remove a space so it looks right when printed. (if you want it to print all of the occurences removed just remove count from this method)

s = '@VirginAmerica it was amazing, and arrived an hour early.'
re.sub(r '@(\w+) ?', '', s, count = 1)
#it was amazing, and arrived an hour early.

One other point to note, is that there is no test case where there is no '@' in the sentence.

#! /usr/bin/env python3

TESTS = ['@VirginAmerica it was amazing, and arrived an hour early',
   'heyyyyy@VirginAmerica , am I dreaming?',
   'heyyyyy @VirginAmerica , am I dreaming?',
   '',
   '@United...',
   '@United@VirginAmerica',
   'no-at-word'
]

def removeMarkedWords(sentence):
   # A word ends with
WORD_END = ' \t\r\n,.;:<>?/+!@#$%^&*()|\}][{\"\'='
result = ''

# is there an @word ?
   at_location = sentence.find('@')
if (at_location == -1):
   result = sentence
else:
   while (at_location != -1):
      if (at_location > 0):
         result += sentence[0: at_location] # keep the sentence prefix(before the '@')
sentence = sentence[at_location: ] # remove the prefix
else:
   # The sentence begins '@something...'
# Find the end of the @word by finding the first non - word letter
index = 1
while (index < len(sentence) and sentence[index] not in WORD_END):
   index += 1
# trim off the @word(and
   throw it away)
sentence = sentence[index: ]
#print("DEBUG sentence = [" + sentence + "]")
# is there another @word ?
   at_location = sentence.find('@')
if (at_location == -1):
   result += sentence # no more @words, just keep the tail
return result

for test in TESTS:
   print("[%s]->[%s]" % (test, removeMarkedWords(test)))

Giving the result:

[@VirginAmerica it was amazing, and arrived an hour early] - > [it was amazing, and arrived an hour early]
[heyyyyy @VirginAmerica, am I dreaming ? ] - > [heyyyyy, am I dreaming ? ]
[heyyyyy @VirginAmerica, am I dreaming ? ] - > [heyyyyy, am I dreaming ? ]
[] - > []
[@United...] - > [...]
[@United @VirginAmerica] - > []
[no - at - word] - > [no - at - word]

Suggestion : 3

1 week ago Feb 12, 2022  · In a very similar way to above, we can also get the last word of a string. The difference between getting the first and last word in a string is that now we will be getting … , 4 days ago In this tutorial, we will be solving a program of python get first word in string. We will be discussing the solution to get first word in string in python in 3 ways. Method 1- Python get … , 1 week ago Apr 08, 2020  · Method #1: Using split () Using the split function, we can break the string into a list of words. Use str.split () and list indexing to get the first word in a string. Call str.split () to … , 4 days ago Jun 22, 2022  · So, to get the first N characters of the string, we have to pass 0 as start index and N as the end index i.e. String_value [0 : N] So, it will return characters starting from 0-th …


s = '@VirginAmerica it was amazing, and arrived an hour early.'
t = '[email protected] , am I dreaming?'
m = 'heyyyyy @VirginAmerica , am I dreaming?'
u = ''
f = '@United...'
h = '@[email protected]'

import re s = '@VirginAmerica it was amazing, and arrived an hour early.'
t = '[email protected] , am I dreaming?'
m = 'heyyyyy @VirginAmerica , am I dreaming?'
u = ''
f = '@United...'
h = '@[email protected]'
def find_match(str): res = re.search('@(\w+)', str) if not res: return ''
return res.group(1) def sub_match(str): return re.sub('^[^@]*@\w+', '', str) assert find_match(s) == 'VirginAmerica'
assert find_match(t) == 'VirginAmerica'
assert find_match(m) == 'VirginAmerica'
assert find_match(u) == ''
assert find_match(f) == 'United'
assert find_match(h) == 'United'
assert sub_match(s) == ' it was amazing, and arrived an hour early.'
assert sub_match(t) == ' , am I dreaming?'
assert sub_match(m) == ' , am I dreaming?'
assert sub_match(u) == ''
assert sub_match(f) == '...'
assert sub_match(h) == '@VirginAmerica'
1._
# define the string my_string = "Python Java Ruby JavaScript"
# Using split() method first_word = my_string.split()[0] # Printing print("First word in my string is", first_word)
# define the string my_string = "Python Java Ruby JavaScript"
# Using split() method first_word = my_string.split()[0] # Printing print("First word in my string is", first_word)
# define the string my_string = "Python Java Ruby JavaScript"
# Using
for loop first_word = ''
for character in my_string: if character != '': first_word = first_word + characterelse: break # Printing print("First word in my string is", first_word)
#
import regex module
import re # define the string my_string = "Python Java Ruby JavaScript"
# Using regex split() first_word = re.split("\s", my_string)[0] # Printing print("First word in my string is", first_word)

Suggestion : 4

Last Updated : 04 Aug, 2022

The original string is: GeeksforGeeks is best
The string after omitting first word is: is best

The original string is: GeeksforGeeks is best
The string after omitting first word is: is best

Suggestion : 5

Here we've first compiled a regular expression, then used it to split a string. Just as Python's split() method returns a list of all substrings between whitespace, the regular expression split() method returns a list of all substrings between matches to the input pattern.,The split() method is perhaps more useful; it finds all instances of the split-point and returns the substrings in between. The default is to split on any whitespace, returning a list of the individual words in a string:,If you would like to find a substring and then split the string based on its location, the partition() and/or split() methods are what you're looking for. Both will return a sequence of substrings.,Regular expressions generalize this "wildcard" idea to a wide range of flexible string-matching sytaxes. The Python interface to regular expressions is contained in the built-in re module; as a simple example, let's use it to duplicate the functionality of the string split() method:

x = 'a string'
y = "a string"
x == y
True
multiline = ""
"
one
two
three
   ""
"
fox = "tHe qUICk bROWn fOx."
fox.upper()
'THE QUICK BROWN FOX.'