This question already has answers here:
How to check if a string only contains letters?
(9 answers)
Closed 8 years ago.
I need to know how to check if a string only contains letters.
example:
name=input("enter your name \n")
while len(name) < 2:
name=input("please enter a valid name \n")
thank you all
You can use str.isalpha()
>>> 'abc'.isalpha()
True
>>> 'abc12'.isalpha()
False
Or if you are expecting a first and last name, and want to ignore the space separator
>>> 'foo bar'.replace(' ', '').isalpha()
True
Related
This question already has answers here:
Print without space in python 3
(6 answers)
Closed 2 years ago.
So whenever I try to run this function there is a space between the two values i want to print can anyone help?
def initials():
dot = "."
f = input("first name: ")
s = input("last name: ")
print(f[0],dot,s[0])
initials()
A trailing comma will result in another space to be appended, but not with '+' operator.
This question already has answers here:
Print without space in python 3
(6 answers)
Closed 4 years ago.
name = input('Enter your name: ')
if len(name) <= 3:
print ('Hi', name, ', you have a short name.')
else:
print('Hi', name, ', you have a long name.')
Why don't you use format?
print("Hi {}, you have a short name".format(name))
You should try using the 'format' function:
print('Hey {name}, you have a nice name'.format(name=name))
You could you the plus operator where-ever you don't need any space.
This question already has answers here:
replace all characters in a string with asterisks
(4 answers)
Closed 4 years ago.
str0 = input("Enter str: ")
str0 = str0.replace(str0, '_')
print(str0)
I need to replace each one of the characters without using loops or conditions
str0 = input("Enter str: ")
str0 = '_'*len(str0)
print(str0)
Should work. The multiplication puts the right amount of _'s so that all of them are replaced.
This question already has answers here:
What is the preferred way to count the number of ocurrences of a certain character in a string?
(6 answers)
Closed 7 years ago.
a=input("Enter the string paragraph:")
count=0
for i in a:
if i==" ":
count=count+1
print("Number of spaces in a string:",count)
Count the number of spacing logical program is working
>>> a=input("Enter the value ")
Enter the value "My Testing String"
>>> a.count(' ')
2
This question already has answers here:
How do I reverse a string in Python?
(19 answers)
Closed 7 years ago.
i want to reverse the order of characters in a string
so take strings as arguments
and return s new string with letters of the original string
example - "hello" would return "olleh"
All I have gotten to is:
def reverse(" "):
string = input("Give me a word")
x = ord(string)
print(x)
You can do it like this:
"hello"[::-1]
You can read about extended-slices here