[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Python Slice question

You are currently reading a thread in /sci/ - Science & Math

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

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

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.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
If a post contains illegal content, please click on its [Report] button and follow the instructions.
This is a 4chan archive - all of the content originated from them. If you need information for a Poster - you need to contact them.
This website shows only archived content and is not affiliated with 4chan in any way.
If you like this website please support us by donating with Bitcoin at 1XVgDnu36zCj97gLdeSwHMdiJaBkqhtMK