find mixed case word in python string

print only the uppercase letters in the string python
how to let python recognize both lower and uppercase input?
python capitalize first letter of every word in string
alternate letters in uppercase in python
python capitalize every other letter
write a python program to capitalize first and last letters of each word of a given string
python check if string contains uppercase
how to capitalize input in python

Some context:

I have never tried to program anything before 3 days ago, so I am a complete beginner. I am trying to learn python, and I am learning through a free module on Grok learning https://groklearning.com/courses/ There is an answer to a question that I have mostly figured out but can't finish. The module is for an introductory course so I am confident that there is a fairly simple solution, but everything ive seen for similar situations was over my head.

The problem is: "Robots in a line!

The robots are invading (your written work)! Robots are sneaking into your text files. Write a program that reads in a line of text from the user and prints out whether there is a robot in the line of text.

If the word robot occurs in all lowercase letters in the line, print out: There is a small robot in the line.

If the word ROBOT occurs in all uppercase letters in the line, print out: There is a big robot in the line.

If the word robot occurs in the line in any combination of upper and lowercase letters, print out: There is a medium sized robot in the line. Otherwise, if none of these conditions hold true, you should print out: No robots here. Your program should work like this:

Line: I'm baking chocolate robot brownies.
There is a small robot in the line. 

Here is another example:

Line: Look at the rOBOt down the road.
There is a medium sized robot in the line.

If the letters robot occur in the line, but as part of a bigger word, you should not report any robots found."

Line: There's a strobotron at the concert.
No robots here.

My solution thus far:

line = input("Write something:")
lr = ("There is a small robot in the line.")
br = ("There is a big robot in the line.")
mr = ("There is a medium sized robot in the line.")
nr = ("No robots here.")
check1 = ("robot" in line)
check2 = ("ROBOT" in line)
lowcase = check1
upcase = check2
if(lowcase == True):
  print(lr)
elif(upcase == True):
  print(br)
else:
  print(nr)

Please remember I am a complete beginner so your solution may need a little bit of explaining, and feel free to critique the code I've already written, but so far it seems to work. Thank you for taking the time to read all this and help.

How about using str.lower or str.casefold:

>>> 'Look at the rOBOt down the road.'.lower()
'look at the robot down the road.'

>>> 'robot' in 'Look at the rOBOt down the road.' # check1
False
>>> 'ROBOT' in 'Look at the rOBOt down the road.' # check2
False
>>> 'robot' in 'Look at the rOBOt down the road.'.lower()
True

if lowcase:
    print(lr)
elif upcase:
    print(br)
elif 'robot' in line.lower():
    print(mr)
else:
    print(nr)

find mixed case word in python string, There are a number of "is methods" on strings. islower() and isupper() should meet your needs: >>> 'hello'.islower() True >>> [m for m in dir(str)  I'm looking for ignore case string comparison in Python. I tried with: if line.find('mandy') >= 0: but no success for ignore case. I need to find a set of words in a given text file. I am reading the file line by line. The word on a line can be mandy, Mandy, MANDY, etc. (I don't want to use toupper/tolower, etc.).

The hardest part is when robot appears in part of a bigger word, since it is easy to lookup for "robot" in the string but abit harder to check if there are boundaries at both side of it.

Using only builtins:

BOUNDARIES = " .,?!:;'\""

while True:
    string = input("Write something or hit Enter to quit: ")
    # This breaks the loop (quits the program) if the user hit
    # an Enter.
    if not string:
        break
    # First we look robot in the lowercased string.
    part = string.lower().partition("robot")
    # If a robot is in the string, the middle part of part
    # is going to be "robot". If not, it is an empty string.
    if not part[1]:
        print("No robots here.")
    # If the end of part[0] and the beginning of part[2] is
    # not in BOUNDARIES then we still have no robots there.
    elif ((not part[0] or not part[0][-1] in BOUNDARIES)
        and (not part[2] or not part[2][0] in BOUNDARIES)):
        print("No robots here.")
    # Now we look for small and big robots in the original
    # string.
    elif "robot" in string:
        print("There is a small robot in the line.")
    elif "ROBOT" in string:
        print("There is a big robot in the line.")
    # If we are here that is because of a medium robot.
    else:
        print("There is a medium sized robot in the line.")

Using regular expressions make it abit shorter/cleaner. However your program will start a little slower because it needs to import the re module first:

import re
PATTERN = r"\b[rR][oO][bB][oO][tT]\b"

while True:
    string = input("Write something or hit Enter to quit: ")
    if not string:
        break
    search = re.search(PATTERN, string)
    if not search:
        print("No robots here.")
    elif search.group() == "robot":
        print("There is a small robot in the line.")
    elif search.group() == "ROBOT":
        print("There is a big robot in the line.")
    else:
        print("There is a medium sized robot in the line.")

Test run:

Write something or hit Enter to quit: I'm baking chocolate robot brownies.
There is a small robot in the line.
Write something or hit Enter to quit: Look at the rOBOt down the road.
There is a medium sized robot in the line.
Write something or hit Enter to quit: There's a strobotron at the concert.
No robots here.
Write something or hit Enter to quit: I have a robot.
There is a small robot in the line.
Write something or hit Enter to quit: The code on it's plate was: "ROBOT 142/1".
There is a big robot in the line.
Write something or hit Enter to quit:

Check if string is upper, lower, or mixed case in Python, Write a Python Program to find sequences of one upper case letter followed by between words starting with capital letters · Python | Pandas Series.str.lower(),  In the source string, the first occurrence of the word “Python” is the 10 th position. However, as we given the start index as 17 and end 35, so find method returned the index of the second occurrence which is 27.

Here is one way of doing it.. just using the things that they teach you in that course. i know its not the smartest way but its simple and it works! :)

# Enter your code for "Robots in a line!" here.
str1 = input('Line: ')
words = str1.split()
if 'ROBOT' in words:
  print('There is a big robot in the line.')
elif 'robot' in words:
  print('There is a small robot in the line.')
elif 'robot' in str1.lower().split():
  print('There is a medium sized robot in the line.')
else:
  print('No robots here.')

Python regex to find sequences of one upper case letter followed by , 2.False- If the string contains 1 or more non-uppercase characters. Examples: Input : string = 'GEEKSFORGEEKS' Output : True Input  I know that I can find a word in a string with. if word in my_string: But I want to find all "word" in the string, like this. counter = 0 while True: if word in my_string: counter += 1 How can I do it without "counting" the same word over and over again?

Just using the Grok lectures
line = input("Line: ")
line1 = line.lower() #mixed case will be converted to lowercase
line = line.split() 
line1 = line1.split() #split line1 to see if the word robot exists

if "robot" in line:
    print("There is a small robot in the line.")
elif "ROBOT" in line:
    print("There is a big robot in the line.")
elif "robot" and "ROBOT" not in line and "robot" not in line1: #checking if lower case, upper case, and mixed case converted robot exist
    print("No robots here.")
else:
    print("There is a medium sized robot in the line.")

isupper(), islower(), lower(), upper() in Python and , The string swapcase() method converts all uppercase characters to lowercase and all lowercase characters to uppercase characters of the given string, and  Definition and Usage. The find() method finds the first occurrence of the specified value. The find() method returns -1 if the value is not found. The find() method is almost the same as the index() method, the only difference is that the index() method raises an exception if the value is not found.

You may try as below, but this involves multiple checks.

s = "I'm baking chocolate roBOT brownies"
lower_text = 'robot'

normal_split = s.split()
lower_split = s.lower().split() 

if lower_text in normal_split:
    # check if lower case robot is present in normal split
    print('There is a small robot in the line.')
elif lower_text.upper() in normal_split:
    # check if upper case robot is present in normal split
    print('There is a big robot in the line.')
elif lower_text not in lower_split:
    # check if atleast any case robot is present in the split
    print('No robots here.')
else:
    # if none of the above matches then medium size robot
    print('There is a medium sized robot in the line.')

Cheers!

Python String swapcase(), We can change the case of characters in Python with the lower and upper methods. String casing. Please see the isupper and islower methods. Python It scans a string and determines if the string has capital letters for each word. It can be  Python string method find() determines if string str occurs in string, or in a substring of string if starting index beg and ending index end are given. Syntax str.find(str, beg=0, end=len(string)) Parameters. str − This specifies the string to be searched. beg − This is the starting index, by default its 0.

Python Lower and Upper: Capitalize String, title() Function in python converts the input string to proper case or title case. that is, all words begin with uppercase and the rest are lowercase. Syntax of lower()  I'm working with Python v2, and I'm trying to find out if you can tell if a word is in a string. I have found some information about identifying if the word is in the string - using .find, but is there a way to do an IF statement. I would like to have something like the following: if string.find(word): print 'success' Thanks for any help.

lower() upper() & title() function in python, Method returns title case string (where first character of the each words are in uppercase, rest of all characters are in lowercase). Example: "Hello  Unicode Python 3. Python 3 handles plain string literals as unicode: >>> string = 'Километр' >>> string 'Километр' >>> string.lower() 'километр' Python 2, plain string literals are bytes. In Python 2, the below, pasted into a shell, encodes the literal as a string of bytes, using utf-8.

Python, Mixed-case to underscore is just like capwords to underscore (the to list of lowercased words # First, see if there are underscores: lw = string.split(x,'_') if  There are times with Python when you need to locate specific information in a string. For example, you may want to know whether a string contains the word Hello in it. One of the essential purposes behind creating and maintaining data is to be able to search it later to locate specific bits of information.