-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathstring_slicing.py
More file actions
21 lines (21 loc) · 894 Bytes
/
string_slicing.py
File metadata and controls
21 lines (21 loc) · 894 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
text = "Hello, Python!"
print("Original string:", text)
print("Character at index 0:", text[0])
print("Character at index 7:", text[7])
print("Last character:", text[-1])
print("Second last character:", text[-2])
print("\nSlicing examples:")
print("Substring from index 0 to 5:", text[0:5])
print("Substring from index 7 to end:", text[7:])
print("Substring from start to index 5:", text[:5])
print("Whole string using slice:", text[:])
print("\nSlicing with step values:")
print("Every second character:", text[::2])
print("Reversed string:", text[::-1])
print("Substring with step of 3:", text[::3])
print("\n--- User Input Example ---")
user_string = input("Enter a string: ")
start = int(input("Enter start index: "))
end = int(input("Enter end index: "))
step = int(input("Enter step value: "))
print("Sliced string:", user_string[start:end:step])