All Go Rust Python PHP JavaScript
Chrome Dev Summit to secure your spot in workshops, office hours and learning lounges!

How to Remove all Whitespace in a String in Python

In Python, there are 3 ways to remove all whitespace in a string. Using str.replace Function The str.replace(x) function returns a copy of the string with all occurrences of substring old replaced by new. See the following example: #!/usr/bin/p...
pooriabt

How to Delete a List Element by Value in Python

In Python, there are 3 ways to delete a list element by value. Using list.remove Function The list.remove(x) function removes the first item from the list whose value is equal to x. It raises a ValueError if there is no such item. See the following...
Unused

How to Return Dictionary Keys as a List in Python

In Python, there are 3 ways to return dictionary keys as a list. Using keys Function The dict.keys() function returns a view object that displays a list of all the keys in the dictionary in order of insertion. See the following example: #!/usr/...
Sambhav Khandelwal

How to Get File Creation and Modification Dates Times in Python

In Python, there are 3 ways to get file creation and modification dates times. Using os.path Function The os.path.getmtime() function returns the time of last modification of path. The os.path.getctime() function returns the system’s ctime which, o...
Patcher56

How to Get the ASCII Value of a Character as an Int in Python

In Python, using the ord function is the easiest way to get the ASCII value of a character as an int. Using ord Function The ord() function returns an integer representing the Unicode code point of that character. To get the ASCII code of a charact...
Sambhav Khandelwal

How to check if a variable exists in Python

In Python, there are 3 ways to check if a variable exists. Using locals Function The locals() function returns the dictionary of the current local symbol table. #!/usr/bin/python3 vName = 1 if 'vName' in locals(): print("exists") exists ...
Sambhav Khandelwal

How to Copy a Dictionary in Python

In Python, there are 4 ways to copy a dictionary. Using dict Method If you want to copy the dict, you have to do so explicitly with: #!/usr/bin/python3 a = {"a": 1, "b": 2} # Copy b = dict(a) b['a'] = 3 print(a) print(b) {'a': 1, 'b': 2} {'...
Patcher56

How to Move a File in Python

In Python, there are 3 ways to move a file. Using os Module The os.rename() method renames the file or directory src to dst. If dst exists, the operation will fail with an OSError subclass in a number of cases: #!/usr/bin/python3 # Import modu...
aweis

How to Determine if an Object is Iterable in Python

In Python, there are 2 ways to determine if an object is iterable. Using Duck Typing Duck typing is a concept related to dynamic typing, where the type or the class of an object is less important than the methods it defines. When you use duck typing...
Unused

How to Remove Duplicates in List in Python

In Python, there are 3 ways to remove duplicates in list. Using Built-in set Function The built-in set() function returns a new set object, optionally with elements taken from iterable. If you later need a real list again, you can similarly pass the...
pooriabt

How to Create Multiline Comments in Python

In Python, using the triple-quoted strings is the easiest way to create multiline comments. Using triple-quoted Strings String literals can span multiple lines. One way is using triple-quotes: """...""" or '''...'''. You can use triple-quoted strin...
aweis

How to Check for NaN Values in Python

In Python, there are 2 ways to check for NaN values. Using math.isnan Method The math.isnan(x) method returns True if x is a NaN (not a number), and False otherwise. For example, #!/usr/bin/python3 # Import module import math f = float('nan') ...
ada

How to Check the Version of the Interpreter in Python

In Python, there are 3 ways to check what version of the Interpreter is interpreting my script. Using sys.version String A string containing the version number of the Python interpreter plus additional information on the build number and compiler us...
ada

How to Check If a Directory Exists in Python

In Python, there are 3 ways to check if a directory exists. Using os.path.isdir Method You can use os.path.isdir() to check if a directory exists, it really simple. For example, #!/usr/bin/python3 # Import module import os d = "./folder" print...
aweis

How to Get the Filename Without the Extension from a Path in Python

In Python, there are 2 ways to get the filename without the extension from a path. Using pathlib Module You can use .stem from pathlib to get the filename without the extension from a path, it really simple. For example, #!/usr/bin/python3 # Im...
Sambhav Khandelwal