my_str = "Cool Stuffs" #First example first_char_1 = my_str 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.
str_line = "Data is the new fuel" print(str_line) # -> D print(str_line[:1]) # -> D print(str_line[0:1]) # -> D
fruits = ["Banana", "Apple", "Orange", "Papaya"] first_char = fruits print(first_char) # -> B