Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Yaswanth Varma
Page 26 of 31
What is the most efficient string concatenation method in python?
String concatenation is the process of joining two or more strings together. It is important to choose the right method for string concatenation when dealing with building output, logging messages, etc. While Python has several methods to concatenate strings, not all the methods perform equally, especially incase of nested loops. In this article, we will explore the different methods of string concatenation in Python. Using Python + Operator The first approach is by using the + operator, which is the straightforward way to concatenate strings. This method is inefficient in loops because it creates a new string object ...
Read MoreHow do I un-escape a backslash-escaped string in Python?
In Python, the backslash-escaped string contains the characters that are followed by backslash(\), which are used as escape characters. For example, represents the literal characters "" and "n" (not a new line). In some scenarios, we need to unescape such a string, i.e, convert these sequences back to their intended special characters or read them as normal text. Python provides several ways to achieve this. In this article, we will explore how to un-escape a backslash-escaped string. Using Python .decode() Method The Python decode() method is used to decode the string using the codec ...
Read MoreHow do I wrap a string in a file in Python?
Wrapping the string means formatting or breaking the string so that it fits within the specified width. It is useful when writing the content to the file, such as logs or console-like outputs. Python provides various ways to achieve string wrapping before writing it to the file using the built-in methods. In this article, we are going to learn how to wrap a string in a file in Python. Using Python textwrap.wrap() Method The textwrap.wrap() method is used to wrap a single paragraph of text. so that every line is at most a specified length. It accepts a text ...
Read MoreHow to Convert String to JSON using Python?
In this article, we are going to learn how to convert a string to JSON in Python. JSON is a lightweight data interchange format that is used to read and write, and store data. Usually, we receive JSON data in the form of a string. To process it using a Python program, we need to convert the JSON string into an object, which can be stored in Python dictionaries and lists. This conversion can be done by using functions provided by the Python JSON module. Using Python json.loads() Function The Python json.loads() function is used to convert the JSON-formatted ...
Read MoreHow to detect vowels vs consonants in Python?
In the English alphabet, letters are categorized into vowels and consonants. The vowels are (a, e, i, o, u), and the remaining alphabetic characters are considered as consonants. In this article, we are going to detect the vowels vs the consonants in Python. Python provides a simple way to solve this problem by using the built-in string methods and logical conditions. Using Python in Operator The in operator is used to check whether the provided assignment character exists in the string. It returns true if it exists; otherwise, it returns false. To detect whether a letter is a ...
Read MoreWhen to use %r instead of %s in Python?
Using %r instead of %s in PythonIn Python, string formatting is done by using the %(formatting operator). The %s uses the str() to convert the value, while the %r uses the repr() to convert values. Both operators look similar, but the difference is that they serve different purposes: %s: It converts the value to a human-readable string. %r: It converts the value that contains the quotes, escape characters, etc. Let's dive into the article to learn when to use %r instead of %s, which helps in debugging and logging. ...
Read MoreHow to find longest repetitive sequence in a string in Python?
Strings are the essential data types used in many real-world problems that involve analysing and manipulating text data. In this article, we are going to learn about finding the longest repetitive sequence in a string. The Repetitive sequence refers to a substring that appears more than once in the given string. For performing this task, Python provides built-in features. Using Suffix Array and LCP A suffix array is used to store all the suffixes of the given string in lexicographic order. In this approach, we will consider the input string and create a list of all ...
Read MoreWhat is an efficient way to repeat a string to a certain length in Python?
In Programming, it is useful to repeat or pad a string up to a certain length. Whether generating the formatted output, filling templates, or being able to repeat the string to a specific length can save time. In this article, we are exploring an efficient way to repeat a string to a certain length. Python provides various ways, such as using multiplication, string slicing, and built-in methods like ljust(), rjust(), and zfill(). Using String Multiplication The first approach is by using the * operator. Here, we are going to repeat the string to a specific length using the ...
Read MoreWhy substring slicing index out of range works in Python?
In many programming languages, trying to access the element that is out of range of a string or list results in the IndexError. But Python behaves differently when it comes to substring slicing. Instead of generating the error, Python handles the out-of-range in slicing operations by adjusting the indices to fit the valid limits. This makes the Python slicing safe and appropriate. Let's dive into the article to understand how slicing with out-of-range indices works in Python with examples. Python slice() Function The Python slice() function is used to return the slice object that can be used ...
Read MoreHow to replace the last occurrence of an expression in a string in Python?
In this article, we are going to learn how to replace the last occurrence of an expression in a string. In Python, String manipulation is a common task, and Python provides the built-in method named replace(). Though we can replace the specified character or substring in a string using this method, it does not directly support replacing the last occurrence. To achieve this we need to use slicing or a regular expression. Using Python rfind() Method The first approach is by using the Python rfind() method searches for the starting index of the last occurrence of the specified substring. Here, we ...
Read More