Member Avatar for Member #531174

Hey guys,

I have some Java code here, which checks whether a word is present in a sentence or not:

import java.io.*;
class search_arrays
{
static void search()throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter a sentence: ");
        String s=br.readLine();
        s+=" ";
        String[] x=new String[s.length()];
        int a=0,b=0;
        for(int i=0;i<x.length;i++)
            {
                if(s.charAt(i)==' ')
                    {
                        x[a]=s.substring(b,i);
                        a++;
                        b=i+1;
                    }
                }
         System.out.println("\nEnter a word you want to search for: ");
         s=br.readLine();
         boolean ans=false;
         for(int i=0;i<x.length;i++)
            {
                if(s.equalsIgnoreCase(x[i]))
                    {
                        ans=true;
                    }
                }
          if(ans)
            {
                System.out.println("\n"+s+" has been entered.");
            }
          else
            {
                System.out.println(s+" has not been entered.");
            }
        }
    }

Now, can this be done in Python? Can anyone give me the basic idea of how I can go about this? (No, this is NOT homework!)

Thanks

Dani AI

Generated

As was asking about translating that Java word-search to Python, two concise patterns cover most needs: tokenize-and-compare (simple and fast) or a regex whole-word match (robust for punctuation and edge cases). was right that this is straightforward; the examples below preserve the original intent while avoiding substring false positives (e.g., 'art' matching 'party').

A simple, practical approach that handles common punctuation and is case-insensitive:

sentence = input("Enter a sentence: ")
word = input("Enter a word to search for: ")

words = [w.strip(".,!?;:()[]\"'").lower() for w in sentence.split()]
if word.lower().strip(".,!?;:()[]\"'") in words:
    print("'{}' has been entered.".format(word))
else:
    print("'{}' has not been entered.".format(word))

For stricter whole-word matching (handles punctuation and regex metacharacters safely):

import re

sentence = input("Enter a sentence: ")
word = input("Enter a word to search for: ")

pattern = r'\b' + re.escape(word) + r'\b'
if re.search(pattern, sentence, flags=re.IGNORECASE):
    print("'{}' has been entered.".format(word))
else:
    print("'{}' has not been entered.".format(word))

Notes and gotchas: str.split() is fine for simple cases but a set of normalized tokens ({...}) gives O(1) lookups for many queries. re.escape() prevents special-character surprises; \b uses word boundaries so underscores count as word characters (behaviour to be aware of). For messy natural-language input (Unicode punctuation, contractions, hyphens) a tokenizer (NLTK/spaCy) is preferable. These Python patterns are shorter, clearer, and less error-prone than manual character scanning.

Recommended Answers

All 2 Replies

Member Avatar for Member #531174

Lol, that was a blind spot! I myslef had replied to the thread that you mentioned! Man, I was just soo blind for a few minutes, I forgot!
Thanks! ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.