Hey so I've been learning Java for a little while and have now started with python. I was watching tutorials on slice and lists and I was wondering how you could print out the last letter of a string. An example would be the last letter of each word in this post, I know you need to turn it to a list. I saw further tutorials but no one really answered my question so I was wondering maybe you dont use slice for this but you use is.alpha?
Many thanks for helping !
>>7848507
maybe
print(yourstring[-1])
will do the job...
just watch thenewboston tutorials. he explains it for total noobs
Thanks for answering but I think that will only give me the last letter of the entire string and not the last letter of each word.
yourstring = """ I want to learn programming"""
#Lets say i want the last letter of each word.
mylist = yourstring.split()
would print(yourstring[-1]) do the job?
>>7848507
ys="Hey so I've been learning Java for a little while and have now started with python. I was watching tutorials on slice and lists and I was wondering how you could print out the last letter of a string. An example would be the last letter of each word in this post, I know you need to turn it to a list. I saw further tutorials but no one really answered my question so I was wondering maybe you dont use slice for this but you use is.alpha?"
ys=ys.split()
ys=map(lambda x:x[-1],ys)
print(ys)
//for the full request
>>7848507
You mean something like this?
Thanks for the answer but is it not possible to do this with slice?
I'm learning with learnpythonthehardway
You should first look on google but w/e since i'm here i'll tell you. Here is an example:
a = "Google it first"
list = []
tmp = ""
for character in a:
if character == " ":
list.append(tmp)
tmp = ""
else:
tmp = tmp + character
for word in list:
print(word[-1])
and that's it. btw indexation in string works like in list. If you want to print the nth character from the right of a string do [-1]
>>7848533
A slice can only return contigous blocks of elements, you can't "skip" elements with it.
@anon, no that just gave the last letter of the entire string. What I am after in your string would be my, ul, ng
@anon Ahaaa, okay many thanks for the help!
>>7848524
see >>7848526,
and to explain it a little bit:
blahblah.split() does exactly what the name suggests: splits a string into a list when it meets a blank space,
map maps a function to a list
lambda is an inline way to declare a function.
here, "map(lambda x:x[-1],ys)" maps the function x->x[-1] to each (string) element of list ys,
and x->x[-1] is just "extracting last character of string x"
>>7848533
slice works only with indices, it doesn't look at the contents of your list/string. So you can split the string with yourstring.split(), which will give you a list of words, then apply the slice thing to each string in the list with a list comprehension.
print [word[-1] for word in post.split()]
-> ['y', 'o', 'e', 'n', 'g', 'a', 'r', 'a', 'e', 'e', 'd', 'e', 'w', 'd', 'h', '.', 'I', 's', 'g', 's', 'n', 'e', 'd', 's', 'd', 'I', 's', 'g', 'w', 'u', 'd', 't', 't', 'e', 't', 'r', 'f', 'a', '.', 'n', 'e', 'd', 'e', 'e', 't', 'r', 'f', 'h', 'd', 'n', 's', ',', 'I', 'w', 'u', 'd', 'o', 'n', 't', 'o', 'a', '.', 'I', 'w', 'r', 's', 't', 'o', 'e', 'y', 'd', 'y', 'n', 'o', 'I', 's', 'g', 'e', 'u', 't', 'e', 'e', 'r', 's', 't', 'u', 'e', '?']
You could make this into a single string with join
print ''.join([word[-1] for word in post.split()])
-> yoengaraeedewdh.IsgsnedsdIsgwudttetrfa.nedeetrfhdns,Iwudontoa.IwrstoeydynoIsgeuteerstue?
Dont know if anyone is still reading this but I managed to do this with slice by using a for loop. Thanks for all the advice guys!