+ 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.

1st Apr 2018, 9:50 PM
Arafatur Rahman
Arafatur Rahman - avatar
9 ответов
+ 5
def splitName(name): for x in range(len(name)): if(name[x]==" "): print(name[x:len(name)]+" "+name[0:x])
1st Apr 2018, 10:31 PM
Tomer Sim
Tomer Sim - avatar
+ 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.
2nd Apr 2018, 2:02 PM
ChaoticDawg
ChaoticDawg - avatar
+ 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
2nd Apr 2018, 5:30 PM
Tomer Sim
Tomer Sim - avatar
+ 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)
2nd Apr 2018, 12:23 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
@ChaoticDawg And what do you think split function does? please don't make people use commands without knowing how they're built.
2nd Apr 2018, 11:43 AM
Tomer Sim
Tomer Sim - avatar
+ 1
thanks tomer
1st Apr 2018, 11:11 PM
Arafatur Rahman
Arafatur Rahman - avatar
+ 1
entername = input() firstname, lastname = entername.split() print(lastname) print(firstname)
2nd Apr 2018, 1:56 PM
Markus Kaleton
Markus Kaleton - avatar
0
any other solutions?
2nd Apr 2018, 12:13 AM
Arafatur Rahman
Arafatur Rahman - avatar
0
okay thanks
2nd Apr 2018, 12:49 AM
Arafatur Rahman
Arafatur Rahman - avatar