Python Articles

Page 31 of 855

How to access a collection in MongoDB using Python?

Priya Mishra
Priya Mishra
Updated on 15-Mar-2026 1K+ Views

MongoDB is a well-known NoSQL database that offers a scalable and flexible approach to store and retrieve data. Using Python with MongoDB enables developers to interact with database collections effortlessly through the PyMongo library. This article explains how to access a MongoDB collection using Python, covering the required steps, syntax, and techniques for connecting to, querying, and manipulating data. Syntax from pymongo import MongoClient # Connect to MongoDB client = MongoClient('mongodb://localhost:27017/') # Access database and collection db = client.database_name collection = db.collection_name # Perform operations collection.insert_one(document) collection.find(query) collection.update_one(filter, update) collection.delete_one(filter) PyMongo ...

Read More

How to prepare a Python date object to be inserted into MongoDB?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 14-Mar-2026 2K+ Views

MongoDB stores dates in ISODate format. PyMongo (the official MongoDB driver for Python) directly supports Python's datetime.datetime objects, which are automatically converted to ISODate when inserted. There are three common ways to prepare date objects for MongoDB. Method 1: Current UTC Timestamp Use datetime.datetime.utcnow() to insert the current UTC time ? import datetime from pymongo import MongoClient client = MongoClient() db = client.test_database result = db.objects.insert_one({"last_modified": datetime.datetime.utcnow()}) print("Date Object inserted") Date Object inserted Method 2: Specific Date Use datetime.datetime(year, month, day, hour, minute) for a fixed date ? ...

Read More

Difference between Python and Bash

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 1K+ Views

Python and Bash are both widely used in automation and scripting, but they serve different purposes. Python is a full-featured, object-oriented programming language, while Bash is a command-line interpreter (shell) designed for running system commands and small scripts. Python Python is a dynamically typed, object-oriented programming language designed to be simple and easy to understand. It supports a rich ecosystem of third-party libraries and is used for web development, data science, automation, AI, and much more. Bash Bash (Bourne Again Shell) is a command-line interpreter and the default user shell on Linux and macOS. It was ...

Read More

Explain difference between == and is operator in Python.

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 360 Views

In Python, == and is are both comparison operators but they check different things. == checks if two objects have the same value (equality), while is checks if two variables point to the same object in memory (identity). == Operator (Equality) The == operator compares the values of two objects. If the values are equal, it returns True, regardless of whether they are stored at different memory locations. is Operator (Identity) The is operator checks whether two variables refer to the exact same object in memory (same id()). Even if two objects have equal values, is ...

Read More

Difference between Python and PHP.

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 579 Views

Python and PHP are both popular programming languages but serve different primary purposes. Python is a general-purpose language used across many domains, while PHP is primarily a server-side scripting language designed for web development. Python Python is a high-level programming language with a large built-in standard library. It was developed by Guido van Rossum, with its first version released in 1990. Python emphasizes clean syntax and readability, making it suitable for a wide range of applications from web development to data science and AI. Example # Python: clean, concise syntax languages = ["Python", "PHP", "Java"] ...

Read More

How to call a function with argument list in Python?

Sarika Singh
Sarika Singh
Updated on 13-Mar-2026 4K+ Views

The purpose of a function is to perform a specific task using code blocks. Functions save time by eliminating unnecessary copying and pasting of code. If you need to make a change, you only update the function in one place rather than searching through your entire program. This follows the DRY (Don't Repeat Yourself) principle in software development. Defining a Function in Python Python functions are created using the following syntax − def function_name(parameters): function body A function is defined using the def keyword followed by the function name and ...

Read More

How to expand tabs in string to multiple spaces in Python?

Sarika Singh
Sarika Singh
Updated on 13-Mar-2026 3K+ Views

In Python, handling white spaces between strings is easy. Sometimes, we may want to add space in a string, but we are not sure exactly how much. Python provides different ways to manage this, and one useful method is the expandtabs() method. Using the expandtabs() Method The expandtabs() method in Python is used to replace tab characters (\t) in a string with spaces. It returns a new string where each \t is replaced with the number of spaces needed to reach the next tab stop. You can control how many spaces are used by passing a tabsize value ...

Read More

How can I remove the ANSI escape sequences from a string in python?

Manogna
Manogna
Updated on 13-Mar-2026 9K+ Views

You can use regexes to remove the ANSI escape sequences from a string in Python. Simply substitute the escape sequences with an empty string using re.sub(). The regex you can use for removing ANSI escape sequences is: (\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]. Example Here's how to create a function to remove ANSI escape sequences − import re def escape_ansi(line): ansi_escape = re.compile(r'(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]') return ansi_escape.sub('', line) # Test with a string containing ANSI escape sequences test_string = '\t\u001b[0;35mSomeText\u001b[0m\u001b[0;36m172.18.0.2\u001b[0m' result = escape_ansi(test_string) print(repr(result)) The output of the above ...

Read More

How would you convert string to bytes in Python 3?

Yaswanth Varma
Yaswanth Varma
Updated on 13-Mar-2026 433 Views

In Python, strings and bytes are two different types of data, where the strings are the sequences of unicode characters used for text representation, while bytes are sequences of bytes used for binary data. Converting strings to bytes is used when we want to work with the raw binary data or perform low-level operations. This can be done by using the built-in encode() method or the bytes() constructor. Using Python encode() Method The Python encode() method is used to convert the string into bytes object using the specified encoding format. Syntax Following is the syntax ...

Read More

What is the Python regular expression to check if a string is alphanumeric?

Sarika Singh
Sarika Singh
Updated on 13-Mar-2026 6K+ Views

In this article, we focus on how to check if a string is alphanumeric using regular expressions in Python. Regular expressions are very useful for pattern matching and validation. To use them, first import the re library, which is included by default in Python. The regular expression ^[a-zA-Z0-9]+$ matches strings that contain only letters (both uppercase and lowercase) and numbers. Let's break down this pattern − ^ − Matches the beginning of the string [a-zA-Z0-9] − Character class that matches any lowercase letter (a-z), uppercase letter (A-Z), or digit ...

Read More
Showing 301–310 of 8,547 articles
« Prev 1 29 30 31 32 33 855 Next »
Advertisements