Get the last 4 characters of a string
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Track title: CC P Beethoven - Piano Sonata No 2 in A
--
Chapters
00:00 Question
00:21 Accepted answer (Score 1018)
00:55 Answer 2 (Score 81)
01:07 Thank you
--
Full question
https://stackoverflow.com/questions/7983...
Accepted answer links:
[slices]: https://docs.python.org/3/reference/expr...
[this Stack Overflow answer]: https://stackoverflow.com/a/509295/81170...
Answer 2 links:
http://codepad.org/S3zjnKoD
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #string
#avk47
--
Track title: CC P Beethoven - Piano Sonata No 2 in A
--
Chapters
00:00 Question
00:21 Accepted answer (Score 1018)
00:55 Answer 2 (Score 81)
01:07 Thank you
--
Full question
https://stackoverflow.com/questions/7983...
Accepted answer links:
[slices]: https://docs.python.org/3/reference/expr...
[this Stack Overflow answer]: https://stackoverflow.com/a/509295/81170...
Answer 2 links:
http://codepad.org/S3zjnKoD
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #string
#avk47
ACCEPTED ANSWER
Score 1058
Like this:
>>> mystr = "abcdefghijkl"
>>> mystr[-4:]
'ijkl'
This slices the string's last 4 characters. The -4 starts the range from the string's end. A modified expression with [:-4] removes the same 4 characters from the end of the string:
>>> mystr[:-4]
'abcdefgh'
For more information on slicing see this Stack Overflow answer.
ANSWER 2
Score 86
str = "aaaaabbbb"
newstr = str[-4:]