Python
1: Print Function
2: Escape Sequence
3: Comments
4: Escape Sequence as Normal Text
5: Print Emoji
6: Python as Calculator
7: Strings Concatenation
8: Input
9: int function
10: Variables
11: String Formatting
12: String Indexing
13: String Slicing
14: Step Argument
15: Some useful function and methods
16: Strip Method
17: Find and Replace method
2: Escape Sequence
3: Comments
4: Escape Sequence as Normal Text
5: Print Emoji
6: Python as Calculator
7: Strings Concatenation
8: Input
9: int function
10: Variables
11: String Formatting
12: String Indexing
13: String Slicing
14: Step Argument
15: Some useful function and methods
16: Strip Method
17: Find and Replace method
Chapter 12: String Indexing
We can extract a single character from a string using indexing. The index starts from 0 for the first character and goes up to n-1 for the last character.
Additionally, we can access characters starting from the end of the string without knowing its length. In this case, the indexing starts from -1 for the last character, -2 for the second last, and so on. An example is provided below.
F | r | e | e | L | e | a | r | n | i | n | g | ||
Indexing from first word | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
Indexing from the last word | -13 | -12 | -11 | -10 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
name = "Free Learning"
print(name[-1]) # printing from last word
Output: g
print(name[1]) # printing from first word
Output: r