How do i put a comma next to a variable [duplicate] - python

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.

Related

Random space when indexing in python [duplicate]

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.

The ".replace" operator isn't working Python [duplicate]

This question already has answers here:
Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?
(3 answers)
Why isn't the replace() function working? [duplicate]
(1 answer)
Closed 2 years ago.
I was testing some mechanics out and ran into an issue, the following program should replace the '+' sight to ' + '. The output of this theoretically should be '20 + 20', but in reality, it's '20+20'. I have no idea why.
string = "20+20"
if string.find(" ") == -1:
string.replace("+", " + ")
print(string)
In order for this to work, you need to reassign the string variable with the result of string.replace as the replace function returns the new string.
string = "20+20"
if string.find(" ") == -1:
string = string.replace("+", " + ")
print(string)

How do I move the second word BEFORE the first? [duplicate]

This question already has answers here:
Reverse the ordering of words in a string
(48 answers)
Closed 4 years ago.
If my variable is:
string = "first second"
How can I change that to print:
second first
annoyingly simple but I can't find a solution for it!
I'd split the string on ' ' and then join it back:
s = 'first second'
tmp = s.split(' ')
result = ' '.join((tmp[1], tmp[0]))

How to count number of space in given string in python [duplicate]

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

how to validate a string with a basic code in python [duplicate]

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

Categories