I currently studying Python coding and I can't wait to master it. I have two weeks now in and I'm doing some exercises. So that's why this question (and other's what I might ask in the future :D ) might be trivial for your.
So, the goal here's to ask first name and last name from the user. After that print first letter from the first name as many times there's characters in that name (Jack = 4*J) and last name in the reserved order (Dullname = emanlluD).
Well I nailed that part, but now I just can't find a resolution how do I print this all in the one line. Because first name comes from the list and the print is:
Give your first name: Jack
Give your last name: Dullname
J
J
J
J
emanlluD
when result should be Jack Dullname = JJJJ emanlluD.
firstname = str(input("Give your first name: "))
lastname = str(input("Give your last name: "))
for i in range(len(firstname)):
print(firstname[0])
def reverse(x):
return x[::-1]
output = reverse(lastname)
print(output)
The problem here is the For loop part. I now it should be function and result should be printed with second output, but I just can't figure out how .sort that result.
Thanks for the help!
Edit: Whoa! That was fast! I really appreciate all the answers! My training code is now working!
CodePudding user response:
Just multiply.
print(firstname[0] * len(firstname), lastname[::-1])
CodePudding user response:
firstname = str(input("Give your first name: "))
lastname = str(input("Give your last name: "))
print(firstname[0] * len(firstname), lastname[::-1])
CodePudding user response:
Try this: print(f"{firstname[0]*len(firstname)} {lastname[::-1]}")
CodePudding user response:
Terve Jyri,
You could do that with for-loop like this
for i in range(len(firstname)):
print(firstname[0], end='')
CodePudding user response:
Are you wanting this:
print(f"{''.join(firstname[0] for _ in firstname)} {lastname[::-1]}")
Output:
Give your first name: Jack
Give your last name: Dullname
JJJJ emanlluD
CodePudding user response:
Just try :
firstname = str(input("Give your first name: "))
lastname = str(input("Give your last name: "))
def reverse(x):
return x[::-1]
output = reverse(lastname)
print(firstname[0] * 4, output)
will give
Give your first name: Jack
Give your last name: Dullname
JJJJ emanlluD
CodePudding user response:
I really learned to love string formatting with f"{VARNAME}"
:
print(f"{firstname[0]*len(firstname)} {lastname[::-1]}")
CodePudding user response:
You can either end the print with your choosing print(firstname[0],end=' ') #space
.
or you can concat the first letter (With space) to the output:
firstname = str(input("Give your first name: "))
lastname = str(input("Give your last name: "))
output=""
for i in range(len(firstname)):
output =firstname[0] " "
def reverse(x):
return x[::-1]
output = reverse(lastname)
print(output)
Another option is to utilize python and get rid of the for loop:
firstname = str(input("Give your first name: "))
lastname = str(input("Give your last name: "))
print(firstname[0] * len(firstname) lastname[::-1]) #Repeath the first letter with the length of the firstname and concat the reversed lastname.
CodePudding user response:
Copying from my comment:
You can use * on strings. Also print can take end as a keyword argument. Replace your loop with
print(firstname[0]*len(firstname), end=" ")