Found 9316 Articles for Python

Why substring slicing index out of range works in Python?

Rajendra Dharmkar
Updated on 30-Sep-2019 07:16:23
Slicing outside the bounds of a sequence (at least for built-ins) doesn't cause an error. Indexing returns a single item, but slicing returns a subsequence of items. So when you try to index a nonexistent value, there's nothing to return; but when you slice a sequence outside of bounds, you can still return an empty sequence. For example:>>> s = [1, 2, 3] >>> s[5:8] []But if you just use an index out of bounds, it'll give you an error:>>> s = [1, 2, 3] >>> s[5] Traceback (most recent call last):   File "", line 1, in IndexError: list index out of range

How to process escape sequences in a string in Python?

Rajendra Dharmkar
Updated on 30-Sep-2019 07:16:59
There are two ways to go about unescaping backslash escaped strings in Python. First is using literal_eval to evaluate the string. Note that in this method you need to surround the string in another layer of quotes. For example:>>> import ast >>> a = '"Hello,world"' >>> print ast.literal_eval(a) Hello, worldAnother way is to use the decode('string_escape') method from the string class. For example,>>> print "Hello,world".decode('string_escape') Hello, world

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

Tarun Chandra
Updated on 07-Dec-2022 08:00:42
In this article, we are going to find out how to remove empty strings from a list of strings in Python.. The first approach is by using the inbuilt method filter(). This method takes input from a list of strings and removes empty strings and returns the updated list. It takes None as the first parameter because we are trying to remove empty spaces and the next parameter is the list of strings. The built-in Python function filter() enables you to process an iterable and extract those elements that meet a specified condition. This action is frequently referred to as ... Read More

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

Rajendra Dharmkar
Updated on 13-Feb-2020 10:42:06
Empty strings are "falsy" which means they are considered false in a Boolean context, so you can just use not string.  examplestring = "" if not string:     print "Empty String!"OutputThis will give the output:Empty String!ExampleIf your string can have whitespace and you still want it to evaluate to false, you can just strip it and check again. For example:string = "   " if not string.strip():     print "Empty String!"OutputThis will give the output:Empty String!

How would you convert string to bytes in Python 3?

Rajendra Dharmkar
Updated on 30-Sep-2019 07:19:27
To convert string to bytes in Python 3, you can use the encode() function from the string class. For example,>>> s = u"HellΘ WΘrld" >>> s.encode('utf-8') 'Hell\xce\x98 W\xce\x98rld'

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

Tarun Chandra
Updated on 07-Dec-2022 07:58:38
In this article, we are going to see how to check if multiple strings exist in another string in Python. Imagine a scenario where we must satisfy 50 requirements in order to work. To determine whether conditions are true, we typically employ conditional statements (if, if−else, elif, nested if). However, with so many conditions, the code grows long and unreadable due to the excessive use of if statements. Python's any() function is the only option for such circumstances, thus programmers must use it. The first approach is by writing a function to check for any multiple strings’ existence and then ... Read More

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

Tarun Chandra
Updated on 07-Dec-2022 07:53:11
In this article, we are going to focus on how to create a long multi−line string in Python. The first approach is by using triple quotes, we should add a triple quote then we can add separate line inside the quotes, the multi-line string is created. Using triple quotes in Python, it is possible to span strings across many lines. It can also be applied to lengthy code comments. Triple quotes can also contain special characters like TAB, verbatim, or NEWLINES. As the name would imply, the syntax consists of three single or double quotes placed consecutively. Example In the ... Read More

How do I execute a string containing Python code in Python?

Tarun Chandra
Updated on 07-Dec-2022 07:49:43
In this article, we are going to find out how to execute a string containing Python code in Python. To execute a string containing Python code we should take the input string as multi-line input using triple quotes, then we will use the inbuilt function exec(). This will take a string as input and returns the output of the code that is present inside the string. The exec() function is used to execute Python programmes dynamically. These programmes can be either strings or object code. If it's a string, it's translated into a series of Python statements that are then ... Read More

How to get a function name as a string in Python?

Tarun Chandra
Updated on 07-Dec-2022 07:46:38
In this article, we are going to find out how to get a function name as a string in Python. The first approach is by using the function property __name__. This property will return the name of the function when the property is called. This is only present in Python 3.x version. Python does not have a main() function, so when the command to run a Python program is given to the interpreter, the level 0 indented code will be executed. Before that, though, it will define a few unique variables. One such unique variable is __name__. The interpreter sets ... Read More

Why do backslashes appear twice while printing in Python?

Rajendra Dharmkar
Updated on 30-Sep-2019 07:00:20
Backslash is an escape character. It is used to escape characters like quotes, double quotes, new line, etc. When we print a string containing a backslash, we see it twice because a backslash is also needed to escape itself. It means that we are telling the interpreter that this backslash is to be used as a backslash only and not as an escape character. For example,print 'hello \ John\'s friends'OUTPUT"hello \ John's friends"
Advertisements