• 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 / Truncate String in Python with String Slicing

Truncate String in Python with String Slicing

June 13, 2022 Leave a Comment

To truncate a string variable in Python, you can use string slicing and create a slice depending on your use case.

Below is a simple example of how to truncate a string with slicing in Python

string_variable = "This is a string of words"

truncated_string_to_10_chars = string_variable[:10] 

print(truncated_string_to_10_chars)

#Output:
This is a 

When working with string variables in Python, the ability to easily be able to change and manipulate the values of our strings can be useful.

One such manipulation is to be able to truncate a string.

To truncate a string in Python, you can use slicing. Slicing works for different data types in Python (strings, lists, tuples, etc.) and you can use slicing to end a string at a desired length.

To create a slice and truncate a string, use the following syntax which will create a new string that returns the string characters from the beginning to the stop – 1 position.

truncated_string string_variable[:stop] #returns string characters from the beginning to stop - 1 position

Below is a simple example of how you can truncate a string to the first 10 characters in Python.

string_variable = "This is a string of words"

truncated_string_to_10_chars = string_variable[:10] 

print(truncated_string_to_10_chars)

#Output:
This is a 

How to Use String Slicing in Python

Slicing is very powerful and allows you to do more than just truncate a string from the end. You can also get substrings of any length and starting position with slicing.

In general, here is how to use string slicing in your Python code.

string_variable[start:stop] #returns string characters from start position to stop - 1 position

string_variable[start:] #returns string characters from start position to the end

string_variable[:stop] #returns string characters from the beginning to stop - 1 position

string_variable[:] #returns the entire string variable

Below are some examples showing these operations in Python.

string_var = "1234567890"

print(string_var[3:6])
print(string_var[3:])
print(string_var[:5])
print(string_var[:])

#Output:
456
4567890
12345
1234567890

There is also an optional step parameter which allows you to skip over certain elements.

string_variable[start:stop:step] #returns string characters from start position to stop - 1 position with step

With the step parameter, you could create a string which gets only the even or odd characters from your string.

string_var = "1234567890"

print(string_var[0:6:2])

#Output:
135

You can also use a negative step to reverse the returned string.

Below shows how you can reverse a string with slicing.

string_var = "1234567890"

print(string_var[::-1])

#Output:
0987654321

Hopefully this article has been useful for you to learn how to use slicing to truncate strings in Python.

Other Articles You'll Also Like:

  • 1.  How to Check if Character is Uppercase in Python
  • 2.  Find Last Occurrence in String of Character or Substring in Python
  • 3.  Remove Empty Lists from List in Python
  • 4.  Python ljust Function – Left Justify String Variable
  • 5.  Python cos – Find Cosine of Number in Radians Using math.cos()
  • 6.  Factorial Program in Python Using For Loop and While Loop
  • 7.  Get Day Name from Datetime in pandas DataFrame
  • 8.  Check if Word is Palindrome Using Recursion with Python
  • 9.  Using Python to Sum the Digits of a Number
  • 10.  Using Python to Find Second Smallest 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