[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Python Slice question

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 14
Thread images: 2

File: Python3-powered_hello-world.png (34KB, 502x270px) Image search: [Google]
Python3-powered_hello-world.png
34KB, 502x270px
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
>>
File: python.png (21KB, 792x878px) Image search: [Google]
python.png
21KB, 792x878px
>>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!
Thread posts: 14
Thread images: 2


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.