python
Get character from a string using index Python
Strings in python are like arrays and you can get the characters from a try using its index in python.
my_str = "We are here to help"
print(my_str[4]) #prints 'r'
print(my_str[7]) #prints 'h'
Output
r
h
h
In the code snippet, there is a variable my_str which contains the characters string. We are getting the characters from the string like we get items from arrays using an index.
If we want to get the 8th character from the given string then we will use the syntax:
my_str[8]
Was this helpful?
Similar Posts
- Python code to get the first character from a string
- Python - String, renove all single character for text
- Frequency of each character in a Python String
- python string indexof - get index of a substring
- Python - regex,replace all character exept A-Z a-z and numbers
- Get a value from DataFrame row using index and column name in pandas
- Get the index of the list inside python for loop