Found 9316 Articles for Python

What is the difference between a python module and a python package?

Rajendra Dharmkar
Updated on 30-Sep-2019 08:33:44
Any Python file is a module, its name being the file's base name/module's __name__ property without the .py extension. A package is a collection of Python modules, i.e., a package is a directory of Python modules containing an additional file. The distinguishes a package from a directory that just happens to contain a bunch of Python scripts. Packages can be nested to any depth, provided that the corresponding directories contain their own file.When you import a module or a package, the corresponding object created by Python is always of type module. This means that the distinction between ... Read More

How do Python modules work?

Rajendra Dharmkar
Updated on 30-Sep-2019 08:34:46
Python has a way to put definitions in a file and use them in a script or in an interactive instance of the interpreter. Such a file is called a module; definitions from a module can be imported into other modules or into the main module (the collection of variables that you have access to in a script executed at the top level and in calculator mode).When you import a module, say `hello`, the interpreter searches for a file named in the directory containing the input script and then in the list of directories specified by the environment variable ... Read More

What is zfill() method in Python?

Rajendra Dharmkar
Updated on 30-Sep-2019 08:35:26
The zfill method is built for left padding zeros in a string. For example:>>> '25'.zfill(6) '000025'We can also use the rjust(width[, fillchar]) method in string class that right justifies the string and pads the left side with given filler char. The default filler char is space but we can provide it '0' as well. You can use it as follows:>>> '25'.rjust(6, '0') '000025'We can also use Python string formatting to achieve this result as follows:>>> print "%06d" % 25 '000025'

How to detect vowels vs consonants in Python?

Rajendra Dharmkar
Updated on 30-Sep-2019 08:36:19
First you should check if the character is an alphabet or not. Then you can create a list of vowels and check if the character is a vowel using this. If not then it must be a consonant. For example, def vowel_or_consonant(c):     if not c.isalpha():         return 'Neither'     vowels = 'aeiou'     if c.lower() in vowels:         return 'Vowel'     else:         return 'Consonant' for c in "hello people":     print c, vowel_or_consonant(c)This will give the output:h Consonant e Vowel l Consonant l Consonant ... Read More

How to delete consonants from a string in Python?

Rajendra Dharmkar
Updated on 30-Sep-2019 08:37:06
It is easiest to use regular expressions for this problem. You can separate multiple characters by "|" and use the re.sub(chars_to_replace, string_to_replace_with, str). We have For example:>>> import re >>> consonants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'] >>> re.sub('|'.join(consonants), "", "Hello people", flags=re.IGNORECASE) "eo eoe"Note: You can also use the [] to create group of characters to replace in regex.If you want to keep only vowels and remove all other characters, you can use an easier version. Note that it'll remove spaces, numbers, etc. ... Read More

How to check if a character in a string is a letter in Python?

Rajendra Dharmkar
Updated on 30-Sep-2019 08:37:50
You can use the isalpha() method from string class. It checks if a string consists only of alphabets. You can also use it to check if a character is an alphabet or not. For example, if you want to check if char at 5th index is letter or not,>>> s = "Hello people" >>> s[4].isalpha() TrueYou can also check whole strings, if they are alphabetic or not. For example,>>> s = "Hello people" >>> s.isalpha() False >>> "Hello".isalpha() True

What is a Python bytestring?

Rajendra Dharmkar
Updated on 30-Sep-2019 08:21:31
A string is a sequence of characters; these are an abstract concept, and can't be directly stored on disk. A byte string is a sequence of bytes - things that can be stored on disk. The mapping between them is an encoding - there are quite a lot of these (and infinitely many are possible) - and you need to know which applies in the particular case in order to do the conversion, since a different encoding may map the same bytes to a different string. For example, the same byte string can represent 2 different strings in 2 different ... Read More

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

Rajendra Dharmkar
Updated on 30-Sep-2019 08:22:16
To create a file-like object (same duck type as File) with the contents of a string, you can use the StringIO module. Pass your string to the constructor of StringIO and then you can use it as a file like object. For example,>>> from cStringIO import StringIO >>> f = StringIO('Hello world') >>> 'Hello world'In Python 3, use the io module. For example,>>> import io >>> f = io.StringIO('Hello world') >>> 'Hello world'Note that StringIO doesn't accept Unicode strings that cannot be encoded as plain ASCII strings.

How to replace \\ with in Python?

Rajendra Dharmkar
Updated on 30-Sep-2019 08:23:04
There are two ways to go about replacing \ with \ or 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 correctly sort a string with a number inside in Python?

Updated on 30-Sep-2019 08:24:05
This type of sort in which you want to sort on the basis of numbers within string is called natural sort or human sort. For example, if you have the text:['Hello1', 'Hello12', 'Hello29', 'Hello2', 'Hello17', 'Hello25'] Then you want the sorted list to be:['Hello1', 'Hello2', 'Hello12', 'Hello17', 'Hello25', 'Hello29'] and not:['Hello1', 'Hello12', 'Hello17', 'Hello2', 'Hello25', 'Hello29'] To do this we can use the extra parameter that sort() uses. This is a function that is called to calculate the key from the entry in the list. We use regex to extract the number from the string and sort on both text and number.  import re ... Read More