Random space when indexing in python [duplicate] - python

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.

Related

How to move the beginning of an input to the and? [duplicate]

This question already has answers here:
How do I get a substring of a string in Python? [duplicate]
(16 answers)
How to move the first letter of a word to the end
(3 answers)
Closed 4 years ago.
Let's say I have an input:
SA3213023215
I want to move the SA to the very end of the input.
How can I do this?
Assuming that SA3213023215 is a string (which input is by default), you could use string slicing:
s = "SA3213023215"
s2 = s[2:] + s[:2]
# yields 3213023215SA

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

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.

Replacing all characters of a given string with a specific single character python with no loops [duplicate]

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.

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