Server Side Programming Articles

Page 668 of 2109

How to convert a string to a list of words in python?

Yaswanth Varma
Yaswanth Varma
Updated on 24-Mar-2026 10K+ Views

Strings are one of the most commonly used data types. In this article, we are going to find out how to convert a string to a list of words in Python. Converting the string into a list is helpful when dealing with user inputs or when we want to manipulate individual words in a string. Python provides several ways to achieve this. Let's explore them one by one. Using Python str.split() Method Python str.split() method accepts a separator as a parameter, splits the string at the specified separator, and returns the result as a list. If we ...

Read More

How do I split a multi-line string into multiple lines?

Yaswanth Varma
Yaswanth Varma
Updated on 24-Mar-2026 7K+ Views

A string is a collection of characters that may be used to represent a single word or an entire phrase. When working with multi-line strings in Python, you often need to split them into individual lines for processing. This article explores three effective methods to accomplish this task. Using str.split() Method The str.split() method is the most straightforward approach. It splits the string at every newline character ('') and returns a list where each item corresponds to a line from the original string. Syntax str.split(separator, maxsplit) Example Let's split a multi-line string ...

Read More

How to Convert String to JSON using Python?

Yaswanth Varma
Yaswanth Varma
Updated on 24-Mar-2026 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 ...

Read More

How can I tell if a string repeats itself in Python?

Tarun Chandra
Tarun Chandra
Updated on 24-Mar-2026 2K+ Views

In Python, you can check if a string is composed of repeating substrings using several approaches. A string repeats itself when it's made up of one or more occurrences of a smaller pattern. Using String Slicing and find() The most elegant approach uses string concatenation and the find() method. By searching for the original string within a doubled version (excluding first and last characters), we can detect if it has a repeating pattern ? def find_repeating_pattern(s): i = (s + s).find(s, 1, -1) return None if i == ...

Read More

How to do string concatenation without \'+\' operator in Python?

Yaswanth Varma
Yaswanth Varma
Updated on 24-Mar-2026 3K+ Views

In this article, we are going to find out how to do string concatenation without the plus operator in Python. Generally, String concatenation is the process of joining two or more strings into one, and the common way to do this is by using the '+' operator. For example, writing "Welcome" + " " + "TP" results in the string "Welcome TP". But here we need to perform the concatenation without the '+' operator. For achieving this, Python provides alternative methods, which we will explore one by one. Using Python join() Method The first approach is ...

Read More

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

Yaswanth Varma
Yaswanth Varma
Updated on 24-Mar-2026 4K+ Views

In Python, string formatting can be done using the % formatting operator. The %s uses str() to convert values, while %r uses repr() to convert values. Understanding when to use each is crucial for debugging and logging. Key Differences %s: Converts the value to a human-readable string using str() %r: Converts the value to its representation using repr(), showing quotes, escape characters, etc. When to Use %r Use %r when you need to see the exact representation of a value, including escape characters, quotes, and whitespace. This is particularly useful for: Debugging ...

Read More

Can we do math operation on Python Strings?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 4K+ Views

Python strings support certain mathematical operations, but they behave differently than numeric operations. You can use + for concatenation, * for repetition, and eval() for evaluating mathematical expressions stored as strings. String Concatenation with + The + operator concatenates strings together to form a single string − string1 = "Lorem " string2 = "Ipsum" result = string1 + string2 print(result) Lorem Ipsum String Repetition with * The * operator repeats a string a specified number of times − string1 = "Hello" result = string1 * 3 print(result) ...

Read More

How to find the nth occurrence of substring in a string in Python?

Yaswanth Varma
Yaswanth Varma
Updated on 24-Mar-2026 5K+ Views

While working with strings, a common requirement is searching for a specific substring within a larger string. Python provides built-in methods that allow you to locate the first occurrence of a substring, but what if the substring appears multiple times and you need to find a specific occurrence? For example, consider the string "Welcome to the Tutorialspoint to". If we look for the second occurrence of the substring "to", using the find() method will only return the first match. We need an approach that searches beyond the first occurrence, counting each one until we reach the nth. In ...

Read More

How to get integer values from a string in Python?

Tarun Chandra
Tarun Chandra
Updated on 24-Mar-2026 12K+ Views

In Python, extracting integer values from strings is a common task when processing text data. Python provides several approaches including the filter() method, regular expressions, and string manipulation techniques. Using filter() and isdigit() Methods The filter() method creates a new iterator by filtering elements based on a condition. When combined with isdigit(), it extracts only the digit characters from a string − text = "There are 20 teams competing in the Premier League" print("The given string is:") print(text) # Extract digits and join them digits = ''.join(filter(str.isdigit, text)) if digits: ...

Read More

How to scan a string for specific characters in Python?

Tarun Chandra
Tarun Chandra
Updated on 24-Mar-2026 4K+ Views

Scanning a string for specific characters is a common task in Python programming. There are several approaches to check if certain characters exist in a string, each suited for different scenarios. Using the 'in' Operator The simplest method is using the in keyword to check if a single character exists in a string. It returns True if the character is found, False otherwise ? text = "Welcome to Tutorialspoint" char = 'T' print(f"String: {text}") print(f"Character to find: '{char}'") print(f"Character found: {char in text}") String: Welcome to Tutorialspoint Character to find: 'T' Character ...

Read More
Showing 6671–6680 of 21,090 articles
« Prev 1 666 667 668 669 670 2109 Next »
Advertisements