search
HomeBackend DevelopmentPython TutorialPython program: Trim string from right side of string
Python program: Trim string from right side of stringSep 08, 2023 pm 06:21 PM
python programString modificationright trim

Python program: Trim string from right side of string

In Python, we have a predefined function rstrip() to remove the characters on the right side. This means it will remove spaces on the right side of the string.

Let us take an example to understand how to trim from the left side of a string.

  • Remove the right string LESS from the given string "WIRELESS" and get the resulting value as "WIRE".

  • In the given string "kingdom", delete the string dom on the right side, and the result value is "king".

grammar

The syntax used in the following examples is −

isspace()

This is a predefined method in Python for allowing whitespace, newlines, or spaces in characters.

rstrip("parameter as a string")

This is a predefined method used in Python that accepts a character as argument and removes that character from the right side of the string.

endswith()

This is a built-in method in Python that returns true if the string ends with a specific value.

Example 1

In this program, we store the input string in the variable ‘str’. The variable 'i' is then initialized to the value 5, and characters after the 5th index will be trimmed later. Next, the variable ‘str’ is iterated over the variable ‘char’ using a for loop. Then use the if statement to search for spaces using the isspace() method. If a space is not found in the string, it breaks the loop and the variable "i" is decremented for each white space character. Now we trim the characters using str[:i] and store the value in variable 'trim_str'. Finally, we use the variable 'trim_str' to print the result.

#trim the string from the right
str = "UNIVERSITY"
i = 5
for char in str:
   if not char.isspace():
      break
   i -= 1
trim_str = str[:i] #The use before slicing removes the right string.
print("Trim the string of", i," characters from right:", trim_str)

Output

Trim the string of 5 characters from right: UNIVE 

Example 2

In this program, we store the input string in the variable ‘my_str’. We then remove the character "a" from the right side of the string and store it in the variable 'trim_str'. Finally, we use the variable 'trim_str' to print the result.

#Trim the string from right
my_str = "aaaaa!King!aaaaa"
trim_str = my_str.rstrip("a")
print(trim_str)

Output

aaaaa!King!
The Chinese translation of

Example 3

is:

Example 3

In this program, we will store the input string in the variable str_name. The correct deletion string is then stored in the variable del_suffix. Then use an if statement to check the condition for removing the right side of the string using the built-in method endswith(). Next, use the replace() method to remove the given string and store it in the variable str_name. Finally, we use the str_name variable to print the output.

str_name = "abcdefghi"
del_suffix = "ghi"
if str_name.endswith(del_suffix):
   str_name = str_name.replace(del_suffix, "")
print("After deleting the suffix from left side:",str_name)

Output

After deleting the suffix from left side: abcdef
The Chinese translation of

Example 4

is:

Example 4

In the following program, we store the input string in the variable s. Then use the built-in method removesuffix() to set a string named 'iop', remove the string from the right and in the print() function.

s = 'qwertyuiop'
print(s.removesuffix('iop'))

Output

qwertyu

in conclusion

We understand the difference between these two examples by trimming the string from the left. We see several different methods used in the examples, including isspace(), rstrip(), endswith(), and slicing techniques. Slicing techniques are often used to trim strings from the right side.

The above is the detailed content of Python program: Trim string from right side of string. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
PyCharm高级教程:利用PyInstaller将代码打包为EXE格式PyCharm高级教程:利用PyInstaller将代码打包为EXE格式Feb 20, 2024 am 09:34 AM

PyCharm是一款功能强大的Python集成开发环境,提供了丰富的功能和工具来帮助开发者提高效率。其中,PyInstaller是一个常用的工具,可以将Python代码打包为可执行文件(EXE格式),方便在没有Python环境的机器上运行。在本篇文章中,我们将介绍如何在PyCharm中使用PyInstaller将Python代码打包为EXE格式,并提供具体的

PyCharm社区版支持的插件足够吗?PyCharm社区版支持的插件足够吗?Feb 20, 2024 pm 04:42 PM

PyCharm社区版支持的插件足够吗?需要具体代码示例随着Python语言在软件开发领域的应用越来越广泛,PyCharm作为一款专业的Python集成开发环境(IDE),备受开发者青睐。PyCharm分为专业版和社区版两个版本,其中社区版是免费提供的,但其插件支持相对专业版有所限制。那么问题来了,PyCharm社区版支持的插件足够吗?本文将通过具体的代码示例

Python元编程:赋予你编程超能力的工具Python元编程:赋予你编程超能力的工具Feb 19, 2024 pm 04:45 PM

python元编程是一种强大的技术,它允许你对Python语言本身进行操作,赋予你编程超能力。元编程可以通过使用元类和装饰器来实现。元类是一种特殊的类,它负责创建其他类。装饰器是一种函数,它可以修改另一个函数的行为。元编程的一个常见用途是创建自定义的类。例如,你可以创建一个元类,它可以生成具有特定属性和方法的类。元编程还可以用于修改类的方法行为。例如,你可以创建一个装饰器,它可以对函数的输入和输出进行验证。元编程是一项强大的技术,它可以让你做很多有趣和有用的事情。如果你想成为一名更强大的Pyth

Geany你是最后知道的!Geany你是最后知道的!Feb 03, 2024 pm 09:24 PM

我们来了解一下吧:Geany是一个小巧的使用GTK+2开发的跨平台的开源集成开发环境,以GPL许可证分发源代码,是免费的自由软件。当前版本:1.31。该软件小巧、启动迅速,界面简洁,功能简单。它支持基本的语法高亮、代码自动完成、调用提示、插件扩展。支持文件类型:C,CPP,Java,Python,PHP,HTML,DocBook,Perl,LateX和Bash脚本。对于写多种语言的程序员来说,拥有Geany可以说是非常的方便了。知道了这么多,是不是迫不及待想要去尝试一下呢?下面让我们开始进行下载

Python程序:从字符串的右侧修剪字符串Python程序:从字符串的右侧修剪字符串Sep 08, 2023 pm 06:21 PM

在Python中,我们有一个预定义的函数rstrip()来删除右侧的字符。这意味着它将删除字符串右侧的空格。让我们举一个例子来理解如何从字符串的左侧修剪。在给定的字符串“WIRELESS”中移除右侧字符串LESS并将结果值得到为“WIRE”。在给定的字符串“kingdom”中,删除右侧的字符串dom,得到结果值为“king”。语法以下示例中使用的语法为−isspace()这是Python中预定义的方法,用于允许字符中的空白、换行符或空格。rstrip("parameterasastri

使用海龟绘制柱状图的Python程序使用海龟绘制柱状图的Python程序Aug 20, 2023 pm 04:57 PM

数据的图形表示提供了对数据复杂子结构的增强理解,帮助我们轻松解释隐藏的模式和趋势。想象一下,如果我们可以通过编程绘制类似的关系,那将是多么方便?Python提供了一个丰富的模块,专门用于执行此类操作,它被称为“turtle”。“turtle”模块是Python内置的库,允许我们在“turtle图形屏幕”上绘制图形。在本文中,我们将使用这个turtle模块创建一个条形图。理解Turtle模块Theturtlemoduleusesavirtualturtleobjecttocreategraphic

Python程序以删除字典中的空值为例Python程序以删除字典中的空值为例Sep 03, 2023 pm 04:45 PM

字典被称为集合数据类型。它们以键值对的形式存储数据。它们是有序的且可变的,即它们遵循特定的顺序并被索引。我们可以更改键的值,因此它是可操纵的或可更改的。字典不支持数据重复。每个键可以有多个与其关联的值,但单个值不能有多个键。我们可以使用字典来执行许多操作。整个机制取决于存储的值。在本文中,我们将讨论可用于从字典中删除“空值”的技术。在开始主要操作之前,我们必须对字典中的值处理有一个深入的了解。让我们快速浏览一下本文的概述。本文分为两部分-第1st部分将重点介绍“空值”的概念及其意义。在第2nd部

Python程序用于测试字符串是否只包含数字和字母Python程序用于测试字符串是否只包含数字和字母Aug 30, 2023 am 08:29 AM

在使用Python处理字符串时,经常需要验证一个字符串是否只包含数字和字母,或者是否包含其他特殊字符。字符串验证在各种场景中都非常重要,比如输入验证、数据处理和过滤。在本文中,我们将探讨一个Python程序,用于测试给定的字符串是否仅包含字母数字字符。我们将讨论有效字符串的标准,提供有效和无效字符串的示例,并介绍使用内置字符串方法解决此问题的高效方法。理解问题在我们开始解决问题之前,让我们先定义一个只包含数字和字母的有效字符串的标准-字符串不应包含任何空格或特殊字符。字符串应由字母数字字符(a-

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software