Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
.split(.') aftermath
#1
I was trying to see if this question has been answered. The first print statement shows a pesky character at the beginning of the 2nd list item. I say character because .strip() didn't work to remove it. The loop removes the character, verified in the 2nd print statement. But is there an easier way? Also, what is this character?
sentences = 'This is a sentence. This is too.'
sentence_list = sentences.split('.')
sentence_list.pop()
print(sentence_list)
for i in range(len(sentence_list)):
    if i > 0:
        sentence_list[i] = sentence_list[i][1:]
print(sentence_list)
Reply
#2
When you strip this string at the period
sentences = 'This is a sentence. This is too.'
There is a space after it. That space will still remain after it split. Because there is a second period, it will split into 3 different elements as well.

You need to strip it after splitting. Strip does not remove all whitespace, it just removes the left and right whitespace and stops when it runs into something other than whitespace on both sides. You can just use the strip method to strip off whitespace as well as a one liner with a list comp
>>> sentences = 'This is a sentence. This is too.'
>>> [sentence.strip() for sentence in sentences.split('.') if sentence]
['This is a sentence', 'This is too']
But you can expand that into a regular for loop as well if you are unfamiliar with list comps
s = []
for sentence in sentences.split('.'):
    if sentence:
        s.append(sentence.strip())
Recommended Tutorials:
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020