Remove Special Characters, Punctuation and Spaces from a String in Python

Yaswanth Varma
Updated on 02-May-2025 20:34:26

7K+ Views

It is necessary to clean up the strings by removing the unwanted elements such as special characters, punctuation, and spaces. These unwanted characters can be involved in the tasks and cause unexpected results. In this article, we are going to learn more about removing all special characters, punctuation and spaces from a string. Using Python re Module Through the re module, Python provides support for regular expressions. This module contains methods and classes that we can search, match, and modify a string (text content) using regular expressions.ThePython re.sub() method accepts a pattern, a replacement string, and a string as ... Read More

Python Program to Count Nodes in a Binary Tree

AYUSH MISHRA
Updated on 02-May-2025 20:17:24

3K+ Views

In this article, we are going to learn how we can count the total number of nodes in a binary tree in Python, using different approaches. A binary tree is a data structure in which each node can have at most two children. The two children node of a binary tree are called the left child and the right child. We have to calculate the total number of nodes present in the binary tree. Scenario 1 Input: Binary Tree = 1 2 3 4 5 Output: Total number of nodes: 5 Above-given binary tree has five nodes: 1, 2, ... Read More

Convert Single Character to Integer Value in Python

Sakshi Rayu
Updated on 02-May-2025 19:46:40

1K+ Views

In Python, the ord() function converts a given character to the corresponding ASCII (American Standard Code for Information Interchange) integer. The ord() function raises a TypeError if you pass a string value as a parameter. Converting a single alphabet to its integer In the following example, we have converted a character 'A' into its Unicode using the ord() function - my_str='A' result=ord(my_str) print("Unicode of 'A'-", result) Following is an output of the above code - Unicode of 'A'- 65 Printing Integer values of all the alphabets We can use the ord() function to print all the Unicode characters ... Read More

Open File in Same Directory as Python Script

Niharikaa Aitam
Updated on 02-May-2025 19:38:07

36K+ Views

In Python, it is a common scenario that the files we want to work with should be placed in the same folder as the script itself. Simply we with the reference of a filename like "data.txt" may not always work as expected, especially when the script is executed from a different working directory. The best way is to place the scripts in the actual location using the __file__ variable along with the os or pathlib modules. In this article, we are going to see the different approaches to open a file in the same directory as a Python script. Using ... Read More

Align JRadioButtons Horizontally in Java

Alshifa Hasnain
Updated on 02-May-2025 19:26:39

922 Views

In this article, we will learn to align the JRadioButtons horizontally in Java. While designing graphical user interfaces (GUIs) in Java using Swing, you might frequently have to position radio buttons horizontally instead of the default vertical layout. What is a JRadioButton? A JRadioButton is a subclass of JToggleButton and is a two-state button that can be either selected or deselected. Unlike checkboxes, radio buttons are associated with a group, and only one radio button in a group can be selected. This can be implemented using the ButtonGroup class. Syntax The following is the syntax for JButton initialization: JRadioButton ... Read More

Sort Items of a JComboBox in Java

Alshifa Hasnain
Updated on 02-May-2025 19:26:28

1K+ Views

In this article, we will learn to sort the items of a JComboBox in Java. A JComboBox is a basic Swing component, and one of its common requirements is to show the items in sorted order. What is a JComboBox? A JComboBox is a subclass of the JComponent class, and it is a combination of a text field and a drop-down list from which the user can choose a value. A JComboBox can generate the ActionListener, ChangeListener, and ItemListener interfaces when the user actions with a combo box. Syntax The following is the syntax for JComboBox initialization: JComboBox comboBox ... Read More

Close Resources Automatically in Java

Alshifa Hasnain
Updated on 02-May-2025 19:26:12

607 Views

In this article, we will learn to close resources automatically in Java. Resource management becomes important in Java programming to prevent memory leaks and system instability. Java provides several options for closing resources automatically: files, database connections, and network sockets. The Problem with Manual Resource Closure Traditionally, developers needed to manually close resources using try-finally blocks. This method is error-prone because it's easy to forget to close resources, and exception handling can be complicated: FileInputStream fis = null; try { fis = new FileInputStream("file.txt"); } finally { if (fis != null) { ... Read More

Delete All Files in a Directory with Python

Niharikaa Aitam
Updated on 02-May-2025 19:20:27

32K+ Views

We may see many situations in Python where we need to remove all the files from a particular folder. For example, we may need to perform cleaning up temporary logs, resetting application data, or managing output directories. This can be done by simply pointing to the folder and deleting files manually, but it is inefficient, especially when working with large-scale or automated scripts. In such cases, we can ensure a clean and reliable way of deleting files by using the built-in modules like os, pathlib, or shutil, which allow us ... Read More

Safely Open and Close Files in Python

Niharikaa Aitam
Updated on 02-May-2025 19:03:11

3K+ Views

When working with files in Python programming, the common tasks we can perform are reading data, writing logs or processing content. If the files are not managed in a proper way then it leads to issues such as resource leaks, file corruption or unexpected errors. In this article, we'll explore the different ways to safely open and close files in Python. Why file safety is important? When we open a file in python then the operating system will allocate resources to handle the file. If the file is not closed correctly, then those resources might not be released properly which ... Read More

Difference Between cerr and cout Streams in C++

Akansha Kumari
Updated on 02-May-2025 18:48:58

4K+ Views

cout is an object of the stdout stream, while cerr is an object of the stderr stream.stdout and stderr are different streams, even though they both refer to console output by default. Redirecting (piping) one of them (e.g., program.exe >out.txt) would not affect the other. These are both provided by the library in C++. In this article, we will learn the difference between these two output streams in more detail. Character Output Stream (cout) The character output stream is used to display the data or information to the console (standard output device, basically the screen), like printing messages, results, ... Read More

Advertisements