How to include variables in a string? [duplicate] - python

This question already has answers here:
String formatting: % vs. .format vs. f-string literal
(16 answers)
Closed 7 years ago.
I'm doing some coursework and I need to determine a character's name. This is what I have so far:
charOne=input("Please input your first character's name: ")
charTwo=input("Please input your second character's name: ")
So the user inputs the names, and now I need to ask the user to choose one of these characters.
chooseCharacter=input("What character do you want to use?"
I need to put the users charOne and charTwo into the question. Or some way need to make the user choose the user they want to use.

Use Python string formatting:
charOne = input("Please input your first character's name: ")
charTwo = input("Please input your second character's name: ")
chooseCharacter = input("What character do you want to use? (%s or %s): " % (charOne, charTwo))

Related

how to end code inside an if statement if a letter is typed? [duplicate]

This question already has answers here:
How can I check if character in a string is a letter? (Python)
(6 answers)
How can I read inputs as numbers?
(10 answers)
Closed 10 months ago.
hi i want to end the code if a letter is typed in Cost1 but im not sure how to do that i want it to be only a number in there. any help is appreciated.( the .isletter is just filler for an example)
Item1 = input("What item do you want to buy? ") # asking what item is
if Item1.isdigit():
exit("ERROR")
Cost1 = input("How much does this item cost? ") # asking cost of item
if Cost1.isletter():
exit("ERROR")
from string import ascii_lowercase
if 'a' in ascii_lowercase:
print("letter")

Python case insensitive or sensitive strings as input [duplicate]

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')

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

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