Python case insensitive or sensitive strings as input [duplicate] - python

This question already has answers here:
How do I do a case-insensitive string comparison?
(15 answers)
Closed 1 year ago.
I have the following script:
ip = input('Enter your string: ')
if 'tree' in ip:
print('Correct string')
I would like to print 'Correct string' even if the user input is 'TREE' or 'tree' based on the case. How do I do that (optimally rather than using else or elif)?
If the input from the user is 'TREE' then the script should give an output:
CORRECT STRING
If the input from the user is 'tree' then the script should give an output:
correct string
How do I do that?

Convert the user input to lower case as:
ip = input('Enter your string: ').lower()
if 'tree' in ip:
print('Correct string')

Related

How to display all letters in a string except the first [duplicate]

This question already has answers here:
How do I get a substring of a string in Python? [duplicate]
(16 answers)
Understanding slicing
(38 answers)
Closed 3 years ago.
I want to display an input() string and want to display it so it prints all the letters of the string except the first letter.
string1 = input("enter first string")
string2 = input("enter second string")
print(string1[1] + (string2 - [0]))
I expected it to display as (for examples if string1 was pizza and string 2 was a salad) "palad"
You can use substring. like this:
string1 = input("enter first string")
string2 = input("enter second string")
print(string1[0] + string2[1:])
string2[1:] means: substring from character 2 till the end.

separate output of input() function in Python [duplicate]

This question already has answers here:
How do I split a string into a list of words?
(9 answers)
Closed 4 years ago.
Is there a way to separate the output from the input function in Python?
For example, let's say that we want to insert a number and name.
input('Give number and name:')
Give number and name:14,John
'14,John'
We get '14,John'. Is there a way to take '14','John'?
Thanks in advance.
Does this work?
user_input = input("Please write a number then a name, separated by a comma: ")
number, name = user_input.split(", ")
Use .split()
>>> input('Give number and name: ').split(',')
Give number and name: 14,John
['14','John']
or
>>> number, name = input('Give number and name: ').split(',')
Give number and name: 14,John
>>> number
'14'
>>> name
'John'
Note that they are both strings

If user types numbers instead of letters, show this error [duplicate]

This question already has answers here:
Check if a string contains a number
(20 answers)
Closed 5 years ago.
I'm creating a program where it collects data from the user, I have finished the basic inputs of collecting their first name, surname, age etc; however I wanted the user to have no numbers in their first name or surname.
If the user types a number in their first name such as "Aaron1" or their surname as "Cox2"; it would repeat the question asking for their name again.
Attempt 1
firstname=input("Please enter your first name: ")
if firstname==("1"):
firstname=input("Your first name included a number, please re-enter your first name")
else:
pass
Attempt 2
firstname=input("Please enter your first name: ")
try:
str(firstname)
except ValueError:
try:
float(firstname)
except:
firstname=input("Re-enter your first name: ")
Any suggestions?
First create a function that checks if there are any digits in the string:
def hasDigits(inputString):
return any(char.isdigit() for char in inputString)
Then use a loop to keep asking for input until it contains no digits.
A sample loop would look like the following:
firstname=input("Please enter your first name: ")
while hasDigits(firstname):
firstname=input("Please re-enter your first name (Without any digits): ")
Live Example
You can check if the name contains letters only with isalpha method.
#The following import is only needed for Python 2 to handle non latin characters
from __future__ import unicode_literals
'Łódź'.isalpha() # True
'Łódź1'.isalpha() # False

Python : Input, Raw input error [duplicate]

This question already has answers here:
"NameError: name '' is not defined" after user input in Python [duplicate]
(3 answers)
Closed 6 years ago.
I am trying a simple Hello world, this is my code-
def hello(name=''):
if len(name) == 0 :
return "Hello, World!"
else :
return "Hello, %s!" %(name)
my_name = raw_input()
x = hello(my_name)
print (x)
This code works fine if I use raw_input, but if I use input, it gives an error.
Doesn't the new python not support raw_input.
I also want to know why I defined the parameter in my function as following-
def hello(name='')
Why did I need to use the '' after name
I am really confused, please help. If you have any advice to improve my program, it's appreciated
If you are passing string with input, you have to also mention the double quotes ", for example "My Name"
Whereas in raw_input, all the entered values are treated as string by default
Explanation:
# Example of "input()"
>>> my_name = input("Enter Name: ")
Enter Name: "My Name"
# Passing `"` with the input, else it will raise NameError Exception
>>> my_name
'My Name' <--- value is string
# Example of "raw_input()"
>>> my_name = raw_input("Enter Name: ")
Enter Name: My Name
# Not passing any `"`
>>> my_name
'My name' <--- still value is string

How do I only allow letters when asking for a name in python? [duplicate]

This question already has answers here:
How to check if a string only contains letters?
(9 answers)
Closed 6 years ago.
I am new to coding in python and need to know how to only allow the user to enter letters when inputting a name. So if they input a number or nothing at all, I want the code to say something like "Please only use letters, try again".
Cheers Chris
What you are asking for is a str.isalpha() function:
isalpha(...)
S.isalpha() -> bool
Return True if all characters in S are alphabetic
and there is at least one character in S, False otherwise.
For example you can use it like this:
def ask_name():
while True:
name = raw_input("What is your name?")
if name.isalpha():
return name
else:
print("Please use only letters, try again")

Categories

Resources