From d1c39655e909ad15e9fde47a1f3fe316f0ecd1c5 Mon Sep 17 00:00:00 2001 From: Jeevesh Joshi <64859843+Jeevesh-Joshi@users.noreply.github.com> Date: Sat, 3 Oct 2020 16:48:42 +0530 Subject: [PATCH] Update strings2.py Accessing string values via negative index. --- Versi Lama 03 - strings/strings2.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/Versi Lama 03 - strings/strings2.py b/Versi Lama 03 - strings/strings2.py index 3cef4d32..97fe6a1c 100644 --- a/Versi Lama 03 - strings/strings2.py +++ b/Versi Lama 03 - strings/strings2.py @@ -1,9 +1,22 @@ -# |p|i|s|a|n|g| |g|o| r | e | n | g | -# | | | | | | | | | | | | | | -# 0 1 2 3 4 5 6 7 8 9 10 11 12 13 +# | p | i | s | a | n | g | | g | o | r | e | n | g | + +# | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | #positive indices + +# | -13 | -12 | -11 | -10 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 | #negative indices text = "pisang goreng" a = text[2:] -print('e'+a+' bau') \ No newline at end of file +print('e'+a+' bau') + +# accessing string values via negative index. +if text[-1] == text[12]: + print("True") +else: + print("False") + +""" +string is immutable i.e once created you can not change any of its character. +i.e text[2] == 'A' will give error +"""