×

Python String expandtabs() method

Python String expandtabs() method

The string.expandtabs() method in Python returns a copy of the string where all tab characters are expanded using spaces.

Syntax

String.expandtabs([tabsize])

Parameter

tabsize: This parameter specifies the number of characters to be replaced for a tab character '\t'.

Return

This method returns a copy of the string where all tab characters are expanded using spaces.

Example 1

 # Python program explaining
 # the expandtabs() method
 # no argument is passed as the
 # default tabsize is 8
 str= "Hello World"
 result = str.expandtabs()
 print(result) 

Output

Hello World

Example 2

 # Python program explaining
 # the expandtabs() method
 # initializing string  
 strval = "i\tlove\Tutorialsandexamples"
 # printing the strval
 print(strval)
 # expanding the value 
 print(strval.expandtabs())
 print(strval.expandtabs(12))
 print(strval.expandtabs(14))
 print(strval.expandtabs(120)) 

Output

 i love\Tutorialsandexamples
 i       love\Tutorialsandexamples
 i           love\Tutorialsandexamples
 i             love\Tutorialsandexamples
 i                                                                                                                       love\Tutorialsandexamples 

Example 3

 # Python program explaining
 # the expandtabs() method
 # initializing the string  
 strVal = "i\tlove\Tutorialsandexamples"
 # using expandtabs to insert spacing 
 print("String with default spacing: ", end ="") 
 print(strVal.expandtabs()) 
 # using expandtabs to insert less spacing 
 print("String with less spacing: ", end ="") 
 print(strVal.expandtabs(2)) 
 # using expandtabs to insert more spacing 
 print("String with maximum spacing: ", end ="") 
 print(strVal.expandtabs(12)) 

Output

 String with default spacing: i       love\Tutorialsandexamples
 String with less spacing: i love\Tutorialsandexamples
 String with more spacing: i           love\Tutorialsandexamples 

Related Topics

End Parameter in python

print(): The python print() function prints the program’s output to the output screen. The output can be an integer value, string value or other value. Syntax: print(“hi”) Output: hi will be displayed on the output...

3 minutes read.

Python Time Library

We will consider various functions given by the python module library with examples.This python time module helps to work on time in python to get the current time.Before going with...

4 minutes read.

Poolmanager in Python

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

4 minutes read.

Python String istittle() method

Python String istittle() method The string.istittle() method returns a boolean value true if the string is a titlecased string and there is at least one character, for example uppercase characters may...

2 minutes read.

List Iteration in Python

In this tutorial, we will learn how to iterate list in Python. List in Python A list is an ordered group of values which includes several kinds of values.A list is a mutable...

3 minutes read.

Python Dictionary setdefault() method

Python Dictionary setdefault() method The dictionary.setdefault () method in Python returns the value of the item with the specified key. Syntax dictionary.setdefault(keyname, value) Parameter keyname- This parameter represents the keyname of the item you want...

1 minute read.

Python Dictionary fromkeys() method

Python Dictionary fromkeys() method The dictionary.fromkeys() method in Python creates a new dictionary from the given sequence of elements and returns a dictionary with the specified keys and values. Syntax dictionary.fromkeys(sequence[, value]) Parameter sequence- This...

1 minute read.

Kite Python

Kite in Python: The Kite is a package provided by the python programming language; it works with the help of artificial intelligence and helps us write code inside the visual studio....

3 minutes read.

Cross Validation in Sklearn

Data scientists can benefit from cross-validation in machine learning in two key ways: it can assist in minimising the amount of data needed and ensure the artificial intelligence model is...

14 minutes read.

How to Iterate a List in Python

As we know, a List is one of the four unique data structures available in Python; in this article, we will deep dive into understanding iterating through a list and...

3 minutes read.

Make Notepad using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

5 minutes read.

Python String swapcase() method

Python String swapcase() method The string.swapcase() method in Python returns a copy of the string with uppercase characters converted to lowercase and vice versa. Syntax string.swapcase() Parameter NA Return This method returns a copy of the string...

1 minute read.

Binary Search Visualization using Pygame in Python

A calculation like Binary Search is seen effectively by picturing. Here in this tutorial, a function that pictures the Binary Search Algorithm is carried out. The Graphical User’s Interface (GUI)...

15 minutes read.

Permutations in Python

Recursion Basic idea: for numbers of length N. N-1 items are chosen at random between 0 and then generate permutations using the remaining N-1 elements in a recursive fashion. Once you've done...

4 minutes read.

Python TypeError

What is TypeError in python? TypeError is one of the exceptions in the python programming language. This exception occurs when an operation is performed on an unsupported object type or can...

6 minutes read.

How to Iterate through a Dictionary in Python

In this tutorial, we will learn how to interate throught the dictionary in the Python programming, using different approaches with example. Introduction to Dictionary in Python Dictionary is a special type of...

4 minutes read.

Python Rest API

In this tutorial, we will understand the meaning of API and REST API. We will understand the working of REST API. We will then realize the boundaries of architecture defined...

4 minutes read.

How to Convert String to List In Python?

How to Convert String to List In Python? We all are familiar with what strings and lists are, let us have a quick revision on them- Strings are a sequence of characters...

4 minutes read.

Python JSON

In this tutorial, we will learn about JSON in the Python programming language. We will focus on its features, Guidelines to be kept in mind while writing its syntax, the...

3 minutes read.

Python Not Equal Operator

Python provides us with many operators to make tasks easier. There are about 7 categories of operators in Python. One of the 7 classifications is the comparison operators. Just as...

3 minutes read.