No need for a loop, use list.index
, protected by a try/except
block in case string is not found. list.index
returns the first occurence of the word.
sent="ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY."
words = sent.split()
word = "WHAT"
try:
print(words.index(word)+1)
except ValueError:
print("{} is not in the sentence".format(word))
returns 3
because index
found the word at the 3rd position (and arrays start at 0)
Related Contents:
- Why does comparing strings using either ‘==’ or ‘is’ sometimes produce a different result?
- How to use Stanford Parser in NLTK using Python
- When to use and when not to use Python 3.5 `await` ?
- OpenCV real time streaming video capture is slow. How to drop frames or get synced with real time?
- How to set time limit on raw_input
- How would you make a comma-separated string from a list of strings?
- Convert JSON string to dict using Python
- Pandas groupby: How to get a union of strings
- How can I percent-encode URL parameters in Python?
- What is the difference between contiguous and non-contiguous arrays?
- Logging uncaught exceptions in Python
- How to choose cross-entropy loss in TensorFlow?
- Error: ” ‘dict’ object has no attribute ‘iteritems’ “
- How to import a Python class that is in a directory above?
- Not all parameters were used in the SQL statement (Python, MySQL)
- How do I set the figure title and axes labels font size in Matplotlib?
- Python in Xcode 4+?
- How do I make environment variable changes stick in Python?
- How do you fix the “element not interactable” exception?
- Comments not working in jinja2
- Why does the `is` operator behave differently in a script vs the REPL?
- Can I remove script tags with BeautifulSoup?
- How to add an empty column to a dataframe?
- Cross-platform space remaining on volume using python
- SQLAlchemy: cascade delete
- How does python find a module file if the import statement only contains the filename?
- ElementClickInterceptedException: Message: element click intercepted: Element is not clickable with Selenium and Python
- Poor scaling of multiprocessing Pool.map() on a list of large objects: How to achieve better parallel scaling in python?
- Do something every x (milli)seconds in pygame
- How to recognize whether a script is running on a tty?
- Stop pip from failing on single package when installing with requirements.txt
- In Python, how do I indicate I’m overriding a method?
- Initialize parameter of method with default value
- Remove whitespaces in XML string
- return eats exception
- how to save a pylab figure into in-memory file which can be read into PIL image?
- How to make Python speak
- “is” operator result: What is happening? [duplicate]
- Python requests, how to limit received size, transfer rate, and/or total time?
- Passing output of a CNN to BILSTM
- Access to value of variable with dynamic name
- Simple Python Multiprocessing function in Spyder doesn’t output results
- How can I access global variable inside class in Python
- Concatenate sparse matrices in Python using SciPy/Numpy
- Why do HTTPS requests produce SSL CERTIFICATE_VERIFY_FAILED error?
- Concise vector adding in Python? [duplicate]
- PyAudio IOError: No Default Input Device Available
- What are the pros and cons between get_dummies (Pandas) and OneHotEncoder (Scikit-learn)?
- Use xml.etree.ElementTree to print nicely formatted xml files [duplicate]
- multipart data POST using python requests: no multipart boundary was found