TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to PYTHON

Python Right String Part

Get the right part of strings by using a negative starting slice index. The syntax is shown.
String right. A string contains many characters. Often we want just the last several characters—the ending, rightmost part of the string.Strings
With a special method, we can get just the right part. But often using slice syntax directly is a better choice as it involves less code.
An example. Here we introduce a "right" method. Pay close attention to the return value of right(). It uses slice syntax with a negative start, and no end.

Tip: We do not specify an end—this means the slice continues to the end of the string.

And: We use a negative start—this means we start from that many characters from the end of the string.

Python program that gets right part of strings def right(value, count): # To get right part of string, use negative first index in slice. return value[-count:] # Test the method. source = "soft orange cat" print(right(source, 3)) print(right(source, 1)) # We can avoid the "right" method entirely. # ... We can also use len in the expression. print(source[-2:]) print(source[len(source) - 2:]) Output cat t at at
Some notes. If you are using strings in Python, learning the slice syntax is essential. We get "right" and "left" parts as slices.Substring
No substring method. There is no substring() or right() method in Python. With slice indexes (some of which can be omitted) we get string parts.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf