Get first character of first String in a List in Python

In this Python tutorial, we will learn how to get the first character of first string in a list.

Table Of Contents

Get the first character of first string in a list using the index positions

It is possible to return the first character from first string in a list of strings, by specifying the index positions. The indexing in Python starts from zero. So, the first string’s index position in a list is 0. If you want to get the first character of the first string, then you have to specify – [0][0] or [0][:1] to select it.

Syntax:

list1[0][0]

(or)

list1[0][:1]

Where list1 is a list of strings. Here, list1[0] selects the first string from the list and list1[0][0] or list1[0][:1] gives the first character from the first string of the list.

Example 1:

Get the first character in the first string from a list of 3 strings.

# Consider the strings in a list
list1=["welcome","to","thisPointer"]

print("List: ",list1)

# Get the first character of the first string from the list1
print(list1[0][0])

Output:

List:  ['welcome', 'to', 'thisPointer']
w

First string in list of strings is “welcome” and the first character in that string is ‘w’.

Example 2:

Get the first character in the first string from a list of 3 strings.

# Consider the strings in a list
list1=["welcome","to","thisPointer"]

print("List: ",list1)

# Get the first character of the first string from the list1
print( list1[0][:1] )

Output:

List:  ['welcome', 'to', 'thisPointer']
w

First string in list of strings is “welcome” and the first character in that string is ‘w’.

Get the first character of first string in a list using indices with the strip()

In Python, string class provides a function strip(). It removes the leading and trailing whitespaces from the string. It basically, returns a copy of string, after striping the whitespaces from both the ends of string.

So, if you want to remove whitespaces from front and end of string and then select first character, then this technique is for you.

Syntax:

list1[0].strip()[0]

(or)

list1[0].strip()[:1]

Where, list1 is the list of strings. Here, we selected the first string from list of strings. Then removed the leading and trailing whitepacess from it and then using [0] or [:1], selected the first character from it.

Example 1:

Get the first character in the first string from a list of three strings.

# Consider the strings in a list
list1=["  welcome   ","to","thisPointer"]

print("List: ",list1)

# Get the first character of the first string from the list1
firstChar = list1[0].strip()[0]

print(firstChar)

Output:

List:  ['welcome', 'to', 'thisPointer']
w

The first string in list of strings is ” welcome ” and the first character in that string after removing leading and trailing whitespaces is ‘w’.

Example 2:

Get the first character in the first string from a list of 3 strings.

# Consider the strings in a list
list1=["  welcome   ","to","thisPointer"]

print("List: ",list1)

# Get the first character of the first string from the list1
firstChar = list1[0].strip()[:1]

print(firstChar)

Output:

List:  ['welcome', 'to', 'thisPointer']
w

The first string in list of strings is ” welcome ” and the first character in that string after removing leading and trailing whitespaces is ‘w’.

Summary

We have seen different ways to return the first character of the first string from the list of strings in Python. Thanks.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top