×

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

Write Dictionary to CSV in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

4 minutes read.

Difference between Perl and Python

Control and presently utilized for a great many undertakings, including framework organization, web improvement, network programming, and GUI improvement, and the sky is the limit from there. About Perl? Perl is a...

4 minutes read.

How to find square root in python

How to find Square Root of a number In Python Python makes a lot of tasks easier by using different functions, modules, and libraires. There is an inbuilt function in Python...

6 minutes read.

Array to String in Python

What is an Array: The idea behind an array is to group several items of the same type, making it easier to locate each element by adding an extra offset to...

4 minutes read.

Difference between Input() and raw_input() functions in Python

There are two input functions in Python that are used for taking input are given below: raw_input()input() What is raw_input() Function? raw_input() function is a built-in function in Python. It is used to...

6 minutes read.

Python Operators

Operators in Python programming An operator is a symbol that operates on one or more operands. An operand is a value or a variable on which we perform the operation....

5 minutes read.

Adding a key-value pair to dictionary in Python

Python dictionaries are collections of unsorted key-value pairs. This article will examine a method for adding new key-value teams to an existing dictionary. Dictionary in Python With the aid of curly brackets...

3 minutes read.

Word frequency Python

Word frequency Python In this tutorial, we will write the Python program to count the occurrence of a word (word frequency) in a given sentence. We will learn all the approaches...

5 minutes read.

Python Parallel Processing

By performing more jobs concurrently, your software may complete more tasks in a shorter amount of time. These aid in solving major issues. The following subjects will be covered in...

2 minutes read.

Python If-else statement

In real life, there are situations where we have to make decisions for a particular circumstance and based on those decisions, and we plan our next move. The same thing...

4 minutes read.

Python min() function

Python min() function returns you the minimum value element in an iterable. We can also use this function to determine the smallest value among various arguments passed to this function. We...

4 minutes read.

Iterate a Dictionary in Python

In this tutorial, we will learn how to Iterate a Dictionary in Python. Dictionary: In Python, a dictionary is an unordered collection of data values that is used to store data values,...

3 minutes read.

EDA in Python

The EDA is the exploratory data analysis; the data scientists mainly use the EDA to understand the main features of the data quickly, the variables in python and the relationship...

6 minutes read.

Python Random shuffle( ) method

The shuffle() is used to change the positions of the elements in the mutable sequences. The shuffle( ) function will change the positions of the elements in the sequence of...

3 minutes read.

First Unique Character in a String Python

This article aims to introduce you to strings and how to create a string in Python, and then we will solve a simple yet exciting DSA (Data Structures and Algorithms)...

3 minutes read.

Check Palindrome in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has...

4 minutes read.

How to check if the dictionary is empty in Python?

What is a dictionary in Python? A directory is a collection of data but data is not ordered. Unlike other data types, it does not hold a single value as its...

3 minutes read.

Colors in Python

Adding colour to your visualisations will help them come to life. Even if you know the colours you want to use, picking good ones and putting them into practise might...

4 minutes read.

Python RegEx

Python RegEx (python regular expressions) is a concept of writing and finding expressions in a pool of characters easily. A Regular expression (RegEx) is a set of characters that defines search...

10 minutes read.

Creating new Database using Python MySQL

In this article, we are going to discuss how to create a new database by connecting Python and MySQL. What is a Database? The places or memory used to secure highly and...

6 minutes read.