• 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 Last Character in String in Python

Get Last Character in String in Python

February 12, 2022 Leave a Comment

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.

string = "This is a string."

last_character = string[-1]

print(last_character)

#Output:
.

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 last character of a string.

In Python, we can easily get the last character of a string using string indexing.

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

string = "This is a string."

last_character = string[-1]

print(last_character)

#Output:
.

Getting the Last n Characters from a String in Python

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

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

string = "This is a string."

last_3_characters = string[-3:]

print(last_3_characters)

#Output:
ng.

Getting the First Character in a String Using Python

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

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

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

string = "This is a string."

first_character = string[0]

print(first_character )

#Output:
T

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

Other Articles You'll Also Like:

  • 1.  How to Rotate a List in Python
  • 2.  Using Python to Flatten Array of Arrays
  • 3.  Using readlines() and strip() to Remove Spaces and \n from File in Python
  • 4.  Replace Character in String in Python
  • 5.  Get pandas Series First Element in Python
  • 6.  Using Python to Count Number of False in List
  • 7.  Get Python Dictionary Values as List
  • 8.  Remove Parentheses from String Using Python
  • 9.  Calculate Sum of Dictionary Values in Python
  • 10.  Using Python to Find Second Largest Value in List

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