On 17/07/2015 17:40, Rob Gaddi wrote:
On Thu, 16 Jul 2015 19:15:38 -0700, craig.sirna wrote:I need help writing a homework program. I'll write it, but I can't figure out how to incorporate what I have read in the book to work in code. The assignment wants us to take a users first, middle and last name in a single input ( name=('enter your full name: )). Then we must display the full name rearranged in Last, First Middle order. I tried to use the search function in Python to locate any spaces in the input. It spot back the index 5 (I used Craig Daniel Sirna) That is correct for the first space, but I can't figure out how to get it to continue to the next space. The indexing process is also a bit confusingto me. I get that I can use len(fullName) to set the length of the index, and how the index is counted, but after that I'm lost. I have emailed my professor a few times, but haven't gotten a response.(online course) Any help would be greatly appreciated.1) Use the interactive console. Set x = 'Craig Daniel Sirna' and play with indexing and slicing it until you really internalize what they mean. x[3], x[-3], x[0:10], x[0:-1]. It's not actually relevant to the problem at hand, but right now is the time in your education to get indexing down cold; skimp on it now and you'll pay for it forever. Should take you about 5 minutes.
I'll throw in something to emphasize a major difference between indexing and slicing.
>>> x = 'Craig Daniel Sirna' >>> x[100] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: string index out of range >>> x[100:] '' >>> x[:100] 'Craig Daniel Sirna'
2) https://docs.python.org/3/library/stdtypes.html#string-methods You can do what you're trying to do, but you're swinging a hammer with a powered nailgun at your feet. Search is an inefficient way to try to split a string into parts based on a delimiter.
Inefficient I don't know about, and mostly don't care about either, but certainly not the cleanest way to code, at least IMHO.
-- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence -- https://mail.python.org/mailman/listinfo/python-list
