Hi,
I am creating a program where which the user can input a string into a variable. The program will then convert the variable into a list. The program is a string to morse code program. It makes a list of a string inputed by the user, then it searches the characters of the list and then performs a function whereby an LED is flashed using morse code depending upon what the user enters. Here is my code so far:
string = raw_input("Enter ")
list_string = list(string)
last_element = len(list_string) -1
print list_string[0]
print list_string[last_element]
Fundamentally, I can work out the first and last element of a list and can print the first and last element onto the screen. However, I need to be able to print all of the elements in between the first element (0) and the last element (last_element -1)
Would I do something like:
print list_string[0: last_element] ???
This doesn't work, any suggestions on the correct way to enter this line of code would be greatly appreciated.
Many thanks in advance
Ibanezmatt13
Finding Elements within a list
6 posts
- Posts: 128
- Joined: Fri Dec 28, 2012 9:49 am
Just iterate through the elements with a for loop?
- Posts: 728
- Joined: Thu Mar 08, 2012 3:32 pm
poing wrote:Just iterate through the elements with a for loop?
Yep
- Code: Select all
for element in string:
will treat string as a list and iterate through each item, assigning the value of element to each successive list member.
...and the 'elements' of string are...?
...will treat the string as a list of characters, of course. And 'assigning the value of element to each successive list member' is most likely the other way around, isn't it?
Another way to put it - some might say more explanatory, some may complain that it's too low-level or non-object-oriented:
- Code: Select all
mystring = "Hello, world!"
for current_char in mystring:
print current_char
...will treat the string as a list of characters, of course. And 'assigning the value of element to each successive list member' is most likely the other way around, isn't it?
Another way to put it - some might say more explanatory, some may complain that it's too low-level or non-object-oriented:
- Code: Select all
mystring = "Hello, world!"
for cur_char_index in range(0, len(mystring)):
print mystring[cur_char_index]
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'
- Posts: 852
- Joined: Thu Nov 01, 2012 12:12 pm
- Location: Dublin, Ireland
-rst- wrote:...and the 'elements' of string are...?![]()
Carbon, Hydrogen, Oxygen and probably some Nitrogen and maybe a dash of Phosphorus and a few trace impurities.
No I mean each character. Or are you asking me
- Code: Select all
len(piece_of_string)
Good point about the non-alphanumeric characters, but I would filter those out if not required in the morse version.
Haha
'Are we talking about strings you use when sewing or maybe guitar strings or...'
What I meant, is that while saying 'iterate over the elements in a string' is technically correct, it might be more educational to point out that the 'elements' of a string are characters and naming the variable something that tells it is a character (instead of the generic 'element') would be a good practice.
What I meant, is that while saying 'iterate over the elements in a string' is technically correct, it might be more educational to point out that the 'elements' of a string are characters and naming the variable something that tells it is a character (instead of the generic 'element') would be a good practice.
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'
- Posts: 852
- Joined: Thu Nov 01, 2012 12:12 pm
- Location: Dublin, Ireland