Found 10400 Articles for Python

How to print a string two times with single statement in Python?

Niharikaa Aitam
Updated on 28-May-2025 15:38:03

246 Views

Printing a String Twice Using Multiplication When we are developing Python, we may want to repeat or duplicate a string for display or formatting purposes. In such cases, Python provides several ways to print a string in defined number of times with a single statement. There are different ways to print a string twice with a single statement in Python. They are - Using Multiplication Operator Using the Concatenation Operator Using the format() Method Using f-string Using join() ... Read More

How to divide a string by line break or period with Python regular expressions?

Rajendra Dharmkar
Updated on 16-Dec-2019 08:57:39

362 Views

The following code splits given string by a period and a line break as followsExampleimport re s = """Hi. It's nice meeting you. My name is Jason.""" result = re.findall(r'[^\s\.][^\.]+', s) print resultOutputThis gives the following output['Hi', "It's nice meeting you", 'My name is Jason']

How can I match the start and end in Python\\\'s regex?

Niharikaa Aitam
Updated on 08-Jun-2025 11:22:07

2K+ Views

In Python, the re module is used to work with regular expressions. In some cases, we need to check whether a string starts or ends with a specific pattern, and then we need to use special regex characters called anchors. Following are the anchors used in regular expressions - ^ − This anchor matches the beginning of a string. $ − This anchor matches the end of a string. Python Regex Methods The following methods from the re module are commonly used to apply start and end matching - ... Read More

How do I do a case-insensitive string comparison in Python?

Niharikaa Aitam
Updated on 10-Jun-2025 16:37:19

4K+ Views

In Python, string comparisons are case-sensitive by default. For example, when we consider the strings "Hello" and "hello" then they are considered as two different strings because of the difference in their letter casing.  Case-insensitive String Comparison in Python In some tasks, such as searching user input, comparing filenames, or processing natural language, it's important to compare strings without considering case differences. To perform string comparison in Python, we have several built-in methods such as lower(), upper(), and casefold(), which help us to normalize the casing of strings before comparing them. ... Read More

How to search and replace text in a file using Python?

SaiKrishna Tavva
Updated on 05-Mar-2025 17:39:07

16K+ Views

File manipulation is a fundamental aspect of programming, especially when dealing with data processing and management. Python offers powerful tools for handling files and text efficiently. A common task is searching for specific text patterns within a file and replacing them with desired content. This article explores several practical methods for searching and replacing text in a file using Python. Basic Text Replacement Let's start with a simple example of searching for a specific word in a file and replacing it with another word. In this particular example, we'll search for the word "old" and replace it with "new" − ... Read More

What is the difference between Python\\'s re.search and re.match?

Niharikaa Aitam
Updated on 09-Jun-2025 09:47:04

2K+ Views

In Python, Regular expressions are also called RegEx, which is a sequence of characters that defines a search pattern. The re module provides several functions to work with patterns in text. This is widely used in Python tasks such as string matching, validation, and manipulation. The most commonly used functions in the "re" module are re.match() and re.search(). These two methods are used to check for the presence of a pattern, but they differ in the way they look for a pattern match within a string. In this article, we are going to see the difference between the Python re.search() ... Read More

What is the simplest way to SSH using Python?

Niharikaa Aitam
Updated on 07-Jun-2025 22:28:13

6K+ Views

SSH is referred as secure shell which is useful to remotely manage computers in a secure manner. To connect to a server, we typically use PuTTy, MobaXTerm or the command-line ssh application. Every Unix, Linux and Mac server includes SSH as standard equipment and it is usable in every data centre. SSH connections have made it feasible for secure remote access to resources, remote command execution, the transmission of software patches and updates and other administrative or management tasks. SSH is used in systems administration and file transfer software as well as to handle routers, server hardware, virtualization platforms and ... Read More

How to perform different commands over ssh with Python?

Niharikaa Aitam
Updated on 20-Jun-2025 19:15:44

2K+ Views

When we want to remotely access and execute commands on another machine then we use the paramiko library in Python. Paramiko is a third-party library that is used to connect and communicate securely with remote machines using the SSH, i.e., Secure Shell protocol. It allows us to execute commands, transfer files, and perform other remote tasks programmatically. To use the Paramiko library, first we need to install it in our local system using the below command - pip install paramiko Example Following is the example, in which we connect to a remote server via SSH and perform multiple shell ... Read More

How to use FTP in Python?

SaiKrishna Tavva
Updated on 13-Nov-2024 12:47:27

416 Views

In Python we can use FTP (File Transfer Protocol) by importing the ftplib module, it will provide a simple interface to access FTP servers to retrieve files and process them locally. ftplib module allows us to write Python programs that perform a variety of automated FTP tasks. Objectives of FTP The objectives of FTP are as follows FTP provides file sharing. FTP helps us to encourage the use of remote computers. FTP is used to transfer the data reliably and efficiently. Some Common tasks we can perform using FTP in Python by utilizing the 'ftplib' module are as follows: ... Read More

How to copy a file to a remote server in Python using SCP or SSH?

Niharikaa Aitam
Updated on 20-Jun-2025 19:16:33

16K+ Views

When we want to transfer files from our local system to a remote server securely, Python provides possible ways to do the file transfer using the Paramiko and SCP libraries. These libraries support SSH-based file transfer, which is secure and reliable. Installing Required Libraries Before we start with file transfer, we need to install all the required libraries with the help of below commands - pip install paramiko scp Using paramiko and SCP/SSH Paramiko is a third-party library available in Python, which is used to transfer a file to a remote server without using an external SCP module. This ... Read More

Advertisements