To extract the nth word from a text string, split the string into words and then pick the word at position n. In Excel or Google Sheets, the shortest working formula is =TRIM(MID(SUBSTITUTE(TRIM(A1)," ",REPT(" ",100)),(n-1)*100+1,100)), where A1 holds the text and n is the word number you want. In a scripting language, split on spaces and index the resulting array. The exact method depends on your tool, so the steps below cover the three most common cases: spreadsheets, Python, and browser or command-line one-liners.
What You'll Need
- The text string you want to work with, ideally in a cell, file, or variable you can reference.
- A spreadsheet app (Excel, Google Sheets, LibreOffice Calc) or a Python installation or a browser console.
- A clear decision on what counts as a "word" - most methods treat any run of characters between spaces as one word, so punctuation stays attached.
Step-by-Step
Method 1: Spreadsheet formula (Excel, Google Sheets, LibreOffice Calc)
Menu labels and function behavior can differ slightly by version and region, but the formula text is the same across all three apps.
- Put your text in cell A1. For example:
The quick brown fox jumps. - Decide which word number you want. Word 3 in that example is
brown. - Click an empty cell and paste this formula, replacing
3with your word number:=TRIM(MID(SUBSTITUTE(TRIM(A1)," ",REPT(" ",100)),(3-1)*100+1,100)). - Press Enter. The cell shows the third word.
- To make it reusable, put the word number in its own cell (say B1) and reference it:
=TRIM(MID(SUBSTITUTE(TRIM(A1)," ",REPT(" ",100)),(B1-1)*100+1,100)). Copy down the column to process many rows. - If your text uses a different separator (a comma, a tab, a semicolon), replace the
" "inside SUBSTITUTE with that character.
How it works: TRIM collapses extra spaces, SUBSTITUTE replaces every single space with 100 spaces, MID grabs a 100-character block starting just after the (n-1)th block, and the outer TRIM strips the padding. The 100 is a safety margin - it must be larger than your longest word.
Method 2: Python
- Open a terminal or your Python editor.
- Assign the text:
text = "The quick brown fox jumps". - Split it:
words = text.split(). With no argument,split()handles any amount of whitespace, including tabs and newlines. - Pick the word. Python lists are zero-indexed, so the third word is index 2:
words[2]returns'brown'. - Guard against short strings:
words[n-1] if len(words) >= n else Noneavoids an IndexError when the text has fewer than n words. - For a one-liner:
"The quick brown fox".split()[2].
Method 3: Browser console or command line
- Open your browser's developer console (usually F12, then the Console tab) or a terminal.
- In JavaScript:
"The quick brown fox".split(/\s+/)[2]. The regex splits on any whitespace run. - In a Unix shell:
echo "The quick brown fox" | awk '{print $3}'prints the third whitespace-separated field. - In PowerShell:
("The quick brown fox" -split '\s+')[2].
Common Mistakes
- Off-by-one errors. Spreadsheet and awk count from 1; Python, JavaScript, and PowerShell arrays count from 0. Word 3 is index 2 in code but position 3 in a formula.
- Assuming one space between words. Double spaces, tabs, and line breaks break naive
split(" ")calls. Usesplit()with no argument in Python,\s+in JavaScript, or wrap the source inTRIMin a spreadsheet. - Forgetting punctuation. Splitting on spaces leaves commas and periods attached:
fox,is one "word." Strip punctuation first if you need clean tokens. - Hard-coding the word number. Embedding
3in a formula makes it unmaintainable; put the index in its own cell or variable. - Ignoring empty strings. An empty cell returns an empty result rather than an error, which can silently break downstream calculations.
- Assuming a 100-character block is always enough. If a single "word" exceeds 100 characters (a URL, for example), the formula truncates it. Increase the number in
REPTand the matching100inMID.
Tips
- Test with a short sample first, then scale to the full column or list.
- Normalize whitespace before splitting:
TRIMin spreadsheets,' '.join(text.split())in Python. - If you need several words, extract them in one pass rather than chaining formulas - a single
split()gives you the whole list. - For very long strings or large datasets, do the work in code rather than in spreadsheet formulas; recalculation gets slow.
- Keep a copy of the original text column so you can re-run extraction with a different rule.
Troubleshooting
If the formula returns #VALUE!, check that the referenced cell actually contains text and that the word number is a positive integer. If it returns an empty string, your index is probably larger than the number of words - count them first. If the result includes extra spaces, the outer TRIM was omitted or your source uses non-breaking spaces, which TRIM does not remove; substitute CHAR(160) for the space in SUBSTITUTE as well. If Python raises IndexError, the string has fewer words than your index - add a length check. If JavaScript returns undefined, the same cause applies. If the output looks truncated mid-word, your block size is too small; raise it. If the wrong word comes back, re-check whether your tool is zero- or one-indexed before changing anything else.
Frequently Asked Questions
Can I extract the last word without knowing how many words there are?
Yes. In Python, text.split()[-1]. In JavaScript, text.trim().split(/\s+/).pop(). In a spreadsheet, use the same MID/SUBSTITUTE pattern with a word count from LEN(TRIM(A1))-LEN(SUBSTITUTE(TRIM(A1)," ",""))+1.
Does this work if words are separated by commas instead of spaces?
Yes, if you change the delimiter. Use split(",") in Python, split(/,/) in JavaScript, or replace the space character in the spreadsheet formula's SUBSTITUTE with a comma.
Why does my formula return the wrong word?
Almost always an indexing mismatch or hidden extra spaces. Trim the source text and confirm whether your tool counts from 0 or 1 before adjusting the formula.
Can I extract the nth word from multiple cells at once?
Yes. Write the formula once with relative references, then drag or copy it down the column. In code, loop over the list of strings and apply the same split-and-index step to each.