Programming Articles

Found 25,445 articles

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 432 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

Handling Exception and use of CX_ROOT directly and subclasses

Sai Nath
Sai Nath
Updated on 13-Mar-2026 430 Views

It is not advisable to use CX_ROOT directly and you would require using one of its direct subclasses. The CX_ROOT is the root class for all exception classes in SAP ABAP, but working with its subclasses provides better control and handling mechanisms. Also, the propagation depends upon the exception subclass hierarchy. Exception Subclass Types There are three main types of exception subclasses, each with different propagation and handling behaviors − Subclasses ...

Read More

Using SAP JCO to connect SAP server to JAVA application

Prabhas
Prabhas
Updated on 13-Mar-2026 2K+ Views

When connecting SAP server to JAVA applications using SAP JCO, it is recommended to use load balancing parameters instead of direct server connection parameters for better reliability and performance. Recommended Connection Parameters Instead of using JCO_AHOST and JCO_SYSNR for direct server connection, use the following load balancing parameters − JCO_R3NAME − Use this with the system ID of the target host JCO_MSHOST − Use this with the message server host name or IP address JCO_MSSERV − Use this with the message server port number ...

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

How to implement a custom Python Exception with custom message?

Manogna
Manogna
Updated on 13-Mar-2026 330 Views

In Python, you can create custom exceptions by inheriting from built-in exception classes. This allows you to define specific error types with meaningful messages for your application. Custom exceptions help make your code more readable and provide better error handling. Creating a Custom Exception Class To implement a custom Python exception with a custom message, you need to create a class that inherits from a built-in exception class like Exception, ValueError, or RuntimeError. The custom class should have an __init__ method to store the custom message. Example Here's how to create and use a custom exception ...

Read More

How can I write a try/except block that catches all Python exceptions?

Manogna
Manogna
Updated on 13-Mar-2026 257 Views

It is a general thumb rule that though you can catch all exceptions using code like below, you shouldn't − try: # do_something() pass except: print("Exception Caught!") However, this will also catch exceptions like KeyboardInterrupt and SystemExit that we may not be interested in handling. This can make it difficult to interrupt your program or cause other unexpected behaviors. Better Approach with Exception Re-raising A better approach is to catch all exceptions but re-raise them after logging or handling. Here's a complete ...

Read More

How to catch StandardError Exception in Python?\\\\\\\\n

Sarika Singh
Sarika Singh
Updated on 13-Mar-2026 608 Views

In Python 2, StandardError was a built-in exception class that served as a base class for all built-in exceptions except for SystemExit, KeyboardInterrupt, and GeneratorExit. Using this class, we were able to catch the most common runtime errors in a single except block. However, since Python 3, the StandardError class has been deprecated, and now all built-in exceptions directly inherit from the Exception class. If you are using Python 3, you should catch exceptions using Exception instead of StandardError. StandardError in Python 2 The StandardError class in Python 2 was designed to catch all standard exceptions that ...

Read More
Showing 1–10 of 25,445 articles
« Prev 1 2 3 4 5 2545 Next »
Advertisements