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)

Advertisements
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.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

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

Scroll to Top