How do I get the first letter of a string in Python?
06May, 2022
How Can We Help?
Created On
byMatt
Print
You are here:
< Back
Maker of websites,
Teacher,
Cat enthusiast
Teacher,
Cat enthusiast
Latest posts by Matt (see all)
- The 2022 End of Year Website Update - 01/01/2023
- Celebrating Ada Lovelace day - 10/10/2022
- Website update 5/9/22 - 05/09/2022
In Python, strings are stored as arrays so if you want the first letter of a string in Python you simply need to address position 0 of the array. Remember that arrays in Python start from 0.
x = "An example"
print(x[0])
As long as you know the position of the character you are looking for you can get any character from a string.
x = "An example"
print(x[3])
In the above example it would return the letter ‘e’.
Matt
administrator
Maker of websites,
Teacher,
Cat enthusiast