my_str = "Cool Stuffs"
#First example
first_char_1 = my_str[0]
print(first_char_1) # -> C
#Second example
first_char_2 = my_str[:1]
print(first_char_2) # -> C
We have introduced two methods in the above code snippet to get the very first character of a string. We are using the character index of the string to get the characters. To get the first character we are using string[0].
str_line = "Data is the new fuel"
print(str_line[0]) # -> D
print(str_line[:1]) # -> D
print(str_line[0:1]) # -> D
fruits = ["Banana", "Apple", "Orange", "Papaya"]
first_char = fruits[0][0]
print(first_char) # -> B
0 Comments