Server Side Programming Articles

Page 670 of 2109

How can I concatenate str and int objects in Python?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 24-Mar-2026 910 Views

In many coding languages, when you try to combine a string with a number, the number is usually converted into a string automatically so both parts can be joined together. This is called implicit type conversion. With the + operator for concatenating a string with a numerical value, Python does not perform the implicit type conversion. Instead, it throws an error (specifically a TypeError) because it expects both operands to be strings. The following are the various methods to do it: Using the str() Function Using f-strings Using ...

Read More

How do I un-escape a backslash-escaped string in Python?

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

In Python, a backslash-escaped string contains characters preceded by a backslash (\) that represent escape sequences. For example, represents literal backslash and "n" characters (not a newline). Sometimes you need to unescape these strings to convert them back to their intended special characters. Python provides several methods to achieve this. Let's explore the most effective approaches. Using the decode() Method The decode() method can convert escape sequences back to their special characters using the unicode_escape encoding ? Syntax string.encode().decode('unicode_escape') Example escaped_string = "Welcome ToTutorialsPoint" result = escaped_string.encode().decode('unicode_escape') print(result) ...

Read More

How to concatenate a string with numbers in Python?

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

Concatenation is the process of joining two or more strings together. In Python, you cannot directly concatenate a string with a number using the '+' operator, as it raises a TypeError due to incompatible data types. Python does not allow implicit conversion between strings and numbers during concatenation. Therefore, we need to explicitly convert numbers to strings before joining them. Using str() Function The str() function converts any value into a string representation. This is the most straightforward approach for string-number concatenation. Syntax str(x) Example Here's how to use the str() ...

Read More

How to format a floating number to fixed width in Python?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 24-Mar-2026 7K+ Views

In Python, formatting a floating point number to a fixed width can be done using methods like Python's f-strings and the flexible format() method. Float Formatting Methods The two built-in float formatting methods are as follows ? f-strings: Convenient way to set decimal places, spacing and separators. str.format(): This method allows formatting types inside the placeholders to control how the values are displayed. ...

Read More

Why substring slicing index out of range works in Python?

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

In many programming languages, trying to access an element that is out of range of a string or list results in an IndexError. But Python behaves differently when it comes to substring slicing. Instead of generating an error, Python handles out-of-range indices in slicing operations by automatically adjusting them to fit within valid limits. This makes Python slicing safe and forgiving ? How Python Handles Out-of-Range Slicing When you use slicing with indices that exceed the string boundaries, Python applies these rules: If the start index is negative and beyond the string start, it's treated ...

Read More

How to process escape sequences in a string in Python?

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

Escape sequences are special characters that represent whitespace characters, quotes, backslashes, or non-printable characters within strings. These sequences start with a backslash followed by a character, for example represents a newline. Sometimes you need to process or interpret these escape sequences in your strings. This article demonstrates different approaches to handle escape sequences in Python. Basic Escape Sequences Let's first see how escape sequences work normally in Python strings − text = "Hello\tEveryoneWelcome to TutorialsPoint" print(text) The output of the above code is − Hello Everyone Welcome to TutorialsPoint ...

Read More

How to remove empty strings from a list of strings in Python?

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

The Lists in Python are used to store collections of items such as numbers, strings. While working with lists of strings, it is common to find empty strings in the list. An empty string is a string value with zero length (""). While they are present as elements in the list, they do not hold any meaningful data and can interfere with operations like sorting and filtering. Removing empty strings ensures the list contains only valid values. Python provides several methods to remove empty strings from a list. Let's explore the most effective approaches ? Using filter() ...

Read More

What is the most elegant way to check if the string is empty in Python?

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

The strings are fundamental data types used to store text. When working with strings, checking if a string is empty is a common task. An empty string is a string with zero characters. There are various ways to check if a string is empty, but choosing elegant approaches helps improve code readability. Using if not string (Most Pythonic) In Python, empty strings are considered falsy, which evaluates to False in a boolean context. Using not string returns True if the string is empty ? my_string = "" if not my_string: print("The ...

Read More

How to check if multiple strings exist in another string in Python?

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

While working with strings, checking if a string exists inside another string is a common task and can be achieved using the in keyword. In this article, we'll explore different methods to check if multiple strings exist in another string in Python. For example, checking if multiple strings like "TP" and "WELCOME" exist in the string "WELCOME to TP". Python provides several approaches including any(), all(), and regular expressions. Using any() Function The any() function returns True if at least one element in an iterable is truthy. We can use it with a generator expression to check ...

Read More

How to create a long multi-line string in Python?

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

In Python, you can create long multi-line strings using several approaches. Each method has its own advantages depending on your specific use case and formatting requirements. Using Triple Quotes The most common approach is using triple quotes (''' or """). This method preserves the exact formatting including line breaks, indentation, and special characters like TAB or NEWLINES ? multiline_string = """Welcome to Tutorialspoint How are you doing? Hope everything is fine Thank you""" print("The multi-line string is:") print(multiline_string) The multi-line string is: Welcome to Tutorialspoint How are you doing? Hope everything is ...

Read More
Showing 6691–6700 of 21,090 articles
« Prev 1 668 669 670 671 672 2109 Next »
Advertisements