From e7abdfdb073fe1d64050913f15844401c876b803 Mon Sep 17 00:00:00 2001 From: Madhumitha Date: Sun, 26 Oct 2025 00:49:20 -0700 Subject: [PATCH] String slicing and indexing --- string slicing and indexing task .py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 string slicing and indexing task .py diff --git a/string slicing and indexing task .py b/string slicing and indexing task .py new file mode 100644 index 00000000..6ca079b9 --- /dev/null +++ b/string slicing and indexing task .py @@ -0,0 +1,20 @@ +# String Slicing and Indexing Example + +# Input a string from the user +text = input("Enter a string: ") + +# Displaying first and last character +print("First character:", text[0]) +print("Last character:", text[-1]) + +# Display a substring (from index 2 to 5) +print("Substring (index 2 to 5):", text[2:6]) + +# Reverse the string +print("Reversed string:", text[::-1]) + +# Print every second character +print("Every 2nd character:", text[::2]) + +# Length of the string +print("Length of the string:", len(text))