• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Get First Character of String in Python

Get First Character of String in Python

February 12, 2022 Leave a Comment

To get the first character of a string using Python, the easiest way is to use indexing and access the “0” position of the string.

string = "This is a string."

first_character = string[0]

print(first_character)

#Output:
T

When working with strings, it can be valuable to be able to easily filter and get only specific values from your list.

One such situation where you may want to get only the first character of a string.

In Python, we can easily get the first character of a string using string indexing. Python strings start with a zero index, and so to get the last character of a string, we can access the “0” position.

Below shows how we can use indexing in Python to get the first character in a string.

string = "This is a string."

first_character = string[0]

print(first_character)

#Output:
T

Getting the First n Characters from a String in Python

In Python, we can easily get the first n characters in a string. To get the first n characters of a string, we can use slicing.

Below is a basic example in Python of how to get the first n characters from a string. We will use 3 for n.

string = "This is a string."

first_3_characters = string[:3]

print(first_3_characters)

#Output:
Thi

Getting the Last Character in a String Using Python

We can modify our first example to easily be able to access the last character in a string in Python.

To get the last character in a string using Python, the easiest way is to use indexing and access the “-1” position of the string.

Below is a basic example in Python of how to get the last character of a string.

string = "This is a string."

last_character = string[-1]

print(last_character)

#Output:
.

Hopefully this article has been useful for you to understand how to get the first character of a string using Python.

Other Articles You'll Also Like:

  • 1.  Difference Between read(), readline() and readlines() in Python
  • 2.  Using Python to Increment Dictionary Value
  • 3.  Create Empty List in Python
  • 4.  Using Python to Get All Combinations of Two Lists
  • 5.  Remove None From List Using Python
  • 6.  pandas percentile – Calculate Percentiles of Series or Columns in DataFrame
  • 7.  Reverse Words in a String Python
  • 8.  Get Month from Datetime in pandas DataFrame
  • 9.  pandas ceil – Find the Ceiling of a Column Using Numpy ceil
  • 10.  How to Slice a Dictionary in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy