Changed to a one-liner that's also more efficient with the strip as Jon Clements suggested in a comment.
with open['Quizzes.txt', 'r'] as f:
wordlist = [line.split[None, 1][0] for line in f]
This is pretty irrelevant to your question, but just so the line.split[None, 1] doesn't confuse you, it's a bit more efficient because it only splits the line 1 time.
From the str.split[[sep[, maxsplit]]]
docs
If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a None separator returns [].
' 1 2 3 '.split[]
returns ['1', '2', '3']
and
' 1 2 3 '.split[None, 1]
returns ['1', '2 3 ']
.
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 first word in string using split[]
- Method 2- Python get first word in string using for loop
- Method 3- Python get first word in string using Regex
- Conclusion
Method 1- Python get first word in string using split[]
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.
String split[] method – The split[] method splits the string into a list. The string is broken down using a specific character which is provided as an input parameter. The space is taken as a default separator.
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
Read also: Python split string into list of characters in 4 Ways
Method 2- Python get first word in string using for loop
This is the simple approach, in which we will iterate over the characters of the string and concatenate the characters until we get the first occurrence of the space character.
Python code-
# 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 + character
else:
break
# Printing
print["First word in my string is", first_word]
Method 3- Python get first word in string using Regex
Regular Expression called regex is a specialized sequence of characters that helps to find a specific string or string set. Python re module provides support for regular expressions. To know about the re module of python you can visit here.
Similar to Python String split[] re module also has a split[] method which split the string at a specific pattern and returns those split strings into a list.
Syntax –
re.split[pattern, string]
To get first word in string we will split the string by space. Hence the pattern to denote the space in regex is “s”.
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]
Output:
First word in my string is Python
Conclusion
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.
I am Passionate Computer Engineer. Writing articles about programming problems and concepts allows me to follow my passion for programming and helping others.
asked Jul 25, 2019 in Python by [45.3k points]
I'm trying to write some code that reads a text file and prints the 1st letter of each line. My current code is:
f=open["testfile1.txt","r"]
for line in f:
words=line.split[]
print[words[0]]
With this, the strings should be split into the individual words, but when I run the code, I get an error message saying list index out of range. I have tried the solutions of similar questions people had on the same topic, but when I use the same code, I get this error. Can anyone explain why this is happening, and how I can fix it? Thanks
1 Answer
answered Jul 25, 2019 by Shubham Rana [16.8k points]
Looks like there are few empty lines, try this, it will work:
f=open["testfile1.txt","r"]
for line in f:
words=line.split[]
if words:
print[words[0]]
f.close[]
Using, with open :
with open["testfile1.txt", "r"] as f:
for line in f:
words = line.split[]
if words:
print[words[0]]