python first characters of string code example
Example 1: parse first characters from string python
first_chars = sample_str[0:3]
print('First 3 characters: ', first_chars)
First 3 characters: Hel
Example 2: python get first character of string
string = 'This is a string'
print(string[0])
Example 3: python char at
>>> s = "python"
>>> s[3]
'h'
>>> s[6]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> s[0]
'p'
>>> s[-1]
'n'
>>> s[-6]
'p'
>>> s[-7]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>>
Example 4: how to get any letter of a string python
Random = "Whatever"
words2 = Random.split(" ")
print('number of letters:')
print(len(Random))
while True:
ask = int(input("what letter do you want?"))
print(Random[ask-1])
Example 5: how to find the first letter of an item in python
mylist[0][0]