+ 1
Ask user his full name and save in string variable.
ask user his full name in the sequence first name spac last name and use a for or while loop to check for spaces in the name, and then print it as last name space first name. please help me write this in python thanks.
9 ответов
+ 5
def splitName(name):
for x in range(len(name)):
if(name[x]==" "):
print(name[x:len(name)]+" "+name[0:x])
+ 3
Tomer Sim obviously I know what the split method does. I was saying that there is no need to create a function, method or loop etc, that is already achieved by using something that is built into the language. Several people who ask questions like these are unaware that these methods exist and my answer wasn't to detract from yours or any other answer given, but to add information for the OP. That being said you shouldn't assume what someone else does or doesn't know.
+ 3
I didn't mean to say that you are unaware of what the function is made of, I meant that people shouldn't use functions without knowing how they're built, not you. sorry
+ 2
You don't really need to use a for or while loop at all, but could just use the split() method, which by default will split a string by its spaces.
name = input("Enter your first and last name: ").split()
print(name[0], end=' ') # first name
print(name[1]) # last name
OR:
first_name, last_name = input("Enter your first and last name: ").split()
print(first_name, last_name)
+ 2
@ChaoticDawg
And what do you think split function does?
please don't make people use commands without knowing how they're built.
+ 1
thanks tomer
+ 1
entername = input()
firstname, lastname = entername.split()
print(lastname)
print(firstname)
0
any other solutions?
0
okay thanks