Why not just iterate through the string?
a_string="abcd"
for letter in a_string:
print letter
returns
a
b
c
d
So, in pseudo-ish code, I would do this:
user_string = raw_input()
list_of_output = []
for letter in user_string:
list_of_output.append(morse_code_ify(letter))
output_string = "".join(list_of_output)
Note: the morse_code_ify function is pseudo-code.
You definitely want to make a list of the characters you want to output rather than just concatenating on them on the end of some string. As stated above, it's O(n^2): bad. Just append them onto a list, and then use "".join(the_list).
As a side note: why are you removing the spaces? Why not just have morse_code_ify(" ") return a "\n"?
Why not just iterate through the string?
a_string="abcd"
for letter in a_string:
print letter
returns
a
b
c
d
So, in pseudo-ish code, I would do this:
user_string = raw_input()
list_of_output = []
for letter in user_string:
list_of_output.append(morse_code_ify(letter))
output_string = "".join(list_of_output)
Note: the morse_code_ify function is pseudo-code.
You definitely want to make a list of the characters you want to output rather than just concatenating on them on the end of some string. As stated above, it's O(n^2): bad. Just append them onto a list, and then use "".join(the_list).
As a side note: why are you removing the spaces? Why not just have morse_code_ify(" ") return a "\n"?
A couple of things for ya:
The loading would be "better" like this:
with file('morsecodes.txt', 'rt') as f:
for line in f:
line = line.strip()
if len(line) > 0:
# do your stuff to parse the file
That way you don't need to close, and you don't need to manually load each line, etc., etc.
for letter in userInput:
if ValidateLetter(letter): # you need to define this
code = GetMorseCode(letter) # from my other answer
# do whatever you want
Videos
First make sure the required number is a valid index for the string from beginning or end , then you can simply use array subscript notation.
use len(s) to get string length
>>> s = "python"
>>> s[3]
'h'
>>> s[6]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> s[0]
'p'
>>> s[-1]
'n'
>>> s[-6]
'p'
>>> s[-7]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>>
In [1]: x = "anmxcjkwnekmjkldm!^%@(*)#_+@78935014712jksdfs"
In [2]: len(x)
Out[2]: 45
Now, For positive index ranges for x is from 0 to 44 (i.e. length - 1)
In [3]: x[0]
Out[3]: 'a'
In [4]: x[45]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
/home/<ipython console> in <module>()
IndexError: string index out of range
In [5]: x[44]
Out[5]: 's'
For Negative index, index ranges from -1 to -45
In [6]: x[-1]
Out[6]: 's'
In [7]: x[-45]
Out[7]: 'a
For negative index, negative [length -1] i.e. the last valid value of positive index will give second list element as the list is read in reverse order,
In [8]: x[-44]
Out[8]: 'n'
Other, index's examples,
In [9]: x[1]
Out[9]: 'n'
In [10]: x[-9]
Out[10]: '7'