• 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
  • VBA
  • 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.  PROC MEANS Equivalent in Python
  • 2.  Pandas Crosstab on Multiple Columns
  • 3.  pandas unique – Get Unique Values in Column of DataFrame
  • 4.  How to Save and Use Styles from Existing Word File With python-docx
  • 5.  How to Find the Longest String in List in Python
  • 6.  pandas covariance – Calculate Covariance Matrix Using cov() Function
  • 7.  Using Python Selenium Webdriver to Refresh Page
  • 8.  How to Cube Numbers in Python
  • 9.  PROC LIFETEST Equivalent in Python
  • 10.  Using Python to Increment Dictionary Value

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy