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

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

Related

How do I print one word at a time from a list I got from user input? [duplicate]

This question already has answers here:
Python split string into multiple string [duplicate]
(3 answers)
Closed 2 years ago.
I ask for a user to input names and I need to return them one a time from a list, but what happens is only single letters are returned instead of the full name.
Names = input("Enter names here: ")
The list I get is something like this,
Jim, John, Sarah, Mark
This is what I try,
print (Names[0])
What I get as a return
J
I want Jim as the return a nothing else.
When input reads the data it will return a string, which you must break into a list. Thankfully strings have a method split just for that. Try:
Names = input("Enter names here: ")
Names = Names.split(',')
You can split the string on spaces or comma and other common delimiters using a simple regular expression. That gives you an array of strings, in case, the words.
Then you chose the first element of the array.
line="Jim, John, Sarah, Mark"
words = re.split(r'[;,\s]\s*', line)
print (words[0])
"Names" variable stores the input as a string. Convert it into a list using:
names_list = Names.split(', ')
print(names_list[0])
However, this will only work if you enter names separated by a comma followed by a space.
A better way is to create an empty list first and then append input elements to the list. Following is an example:
# Creating an empty list
names_list = []
# Iterates till the input is empty or none
while True:
inp = input("Enter name: ")
if inp == "":
break
names_list.append(inp)
# Prints first element of the list
print(names_list[0])

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.

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

How to include variables in a string? [duplicate]

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

Categories

Resources