Search results
May 31, 2009 · If you have an instructor, they probably want you to start with an empty string, and build up a new string from the old one. You can do this with pure syntax and literals using a while loop: def reverse_a_string_slowly(a_string): new_string = ''. index = len(a_string) while index: index -= 1 # index = index - 1.
Literally the first thing? You get the last character of the string, right? So the reverse of a string is the last character, followed by the reverse of everything but the last character, which is where the recursion comes in. The last character of a string can be written as x[-1] while everything but the last character is x[:-1].
Aug 9, 2015 · 3. Less code is usually faster in Python. Luckily, you don't have to guess: python -mtimeit -s"s='x'*100000" "for x in s[::-1]: pass". 100 loops, best of 3: 1.99 msec per loop. python -mtimeit -s"s='x'*100000" "for x in reversed(s): pass". 1000 loops, best of 3: 1.97 msec per loop. python -mtimeit -s"s='x'*100000" "for i in xrange(len(s)-1, 0-1 ...
Sep 9, 2013 · You can do it by using list-comprehension or lambda technique: # Reverse a string without using reverse() function. s = 'Federico'; li = list( s ) #convert string to list. ret = [ li[i-1] for i in xrange(len(li),0,-1) ] #1 liner lambda. print ( "".join( ret ) ) or by doing a backward for loop.
The important parts here are the split function and the join function. To reverse the list you can use reverse(), which reverses the list in place or the slicing syntax [::-1] which returns a new, reversed list.
Dec 19, 2016 · Try y = x[::-1]. This uses splicing to get the reverse of the string. reversed(x) returns an iterator for looping over the characters in the string in reverse order, not a string you can directly compare to x. This is not such a good idea in the case of a large strings and lists.
Feb 7, 2014 · For string[0::-1], it's because you tell Python to start at index 0 and go backwards. Of course it doesn't reverse the string. You should do string[len(string)-1::-1] (or surprisingly also string[len(string)::-1]) to get the string reversed like mhlester has said in his answer. – justhalf. Feb 7, 2014 at 1:37.
I have a string and an arbitrary index into the string. I want find the first occurrence of a substring before the index. An example: I want to find the index of the 2nd I by using the index and str.rfind() s = "Hello, I am 12! I like plankton but I don't like Baseball."
Oct 11, 2013 · I tried coming up with a way to do a reverse string function without using the [ : : -1] method. I'm new to coding and am trying to only use the "primitive" steps. Below is my function and specifications. I was wondering if this is an efficient way to write the function. I appreciate any help. Thanks!
Nov 22, 2013 · def ex1(name): reverseName="". for x in range(len(name)-1,-1,-1): reverseName+=name[x] print reverseName. The print statement prints a newline character (a line break) after each line, this is why you get your characters in vertical. The solution is not to print the character in each loop, but to collect them in a string and print the final ...