Yaswanth Varma

Yaswanth Varma

307 Articles Published

Articles by Yaswanth Varma

Page 6 of 31

How do I wrap a string in a file in Python?

Yaswanth Varma
Yaswanth Varma
Updated on 17-Jun-2025 3K+ Views

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 More

How to Convert String to JSON using Python?

Yaswanth Varma
Yaswanth Varma
Updated on 17-Jun-2025 3K+ Views

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 More

How to detect vowels vs consonants in Python?

Yaswanth Varma
Yaswanth Varma
Updated on 13-Jun-2025 6K+ Views

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 More

When to use %r instead of %s in Python?

Yaswanth Varma
Yaswanth Varma
Updated on 13-Jun-2025 4K+ Views

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 More

How to find longest repetitive sequence in a string in Python?

Yaswanth Varma
Yaswanth Varma
Updated on 10-Jun-2025 3K+ Views

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 More

What is an efficient way to repeat a string to a certain length in Python?

Yaswanth Varma
Yaswanth Varma
Updated on 10-Jun-2025 1K+ Views

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 More

Why substring slicing index out of range works in Python?

Yaswanth Varma
Yaswanth Varma
Updated on 10-Jun-2025 4K+ Views

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 More

How to replace the last occurrence of an expression in a string in Python?

Yaswanth Varma
Yaswanth Varma
Updated on 20-May-2025 8K+ Views

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

How to convert string to binary in Python?

Yaswanth Varma
Yaswanth Varma
Updated on 20-May-2025 1K+ Views

In this article, we are going to learn about converting a string to binary, which means representing each character in the string using the binary digits 0 and 1. A binary number is a number expressed in the base2 numerical system, which uses only two symbols - 0 and 1. Since computers store data as binary, converting the strings into binary helps to understand how data is stored or transmitted. Python provides several methods to achieve this. Using Python ord() and format() Functions The first approach is by using the Python ord() and format(), Where the Python ord() ...

Read More

How to correctly sort a string with a number inside in Python?

Yaswanth Varma
Yaswanth Varma
Updated on 20-May-2025 3K+ Views

Sorting the string that contains the number, such as ("xy1", "xy2", "xy10"),  can be complex in Python. For example, if we sort the list ["xy1", "xy2", "xy10"] using the built-in sort() method, it results in ["xy1", "xy10", "xy2"]. But we will expect "xy2" to come before "xy10". It is because the Python default sorting uses the lexicographical order and compares the characters from left to right based on their Unicode values. Since the character '1' in "xy10" comes before "2" in "xy2", the "xy10" is treated as smaller, even though the number 10 is greater than 2. This ...

Read More
Showing 51–60 of 307 articles
« Prev 1 4 5 6 7 8 31 Next »
Advertisements