×

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

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.

How to Take Input in Python

How To Take Input In Python Python has various in-built functions that help in code redundancy. Let's discuss the usage of some functions- ascii() – it returns a readable representation of objects.format()...

4 minutes read.

Check if the directory exists in Python

To prevent any error, while loading or editing a file, we first have to check that the directory or the file exists or not. This is also done to prevent...

5 minutes read.

Cx_Oracle Python with Example

Python Programming Language: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

6 minutes read.

Flutter with tensor flow in python

Python : Python is an object oriented programming language which is highly interpreted and is highly interactive. Python was created by Guido van Rossum in the year 1985 – 1990 .The...

3 minutes read.

Python Pass Statement

The pass statement is a null statement. The difference between pass and comment is that comment is ignored by the interpreter, whereas pass is not. The pass statement is typically used...

3 minutes read.

JWT Decode Python

Python's PyJWT package makes it possible to encrypt and decrypt JSON Web Tokens (JWT). JWT is a public, recognized global benchmark for safely expressing demands between two parties (RFC 7519). JSON...

7 minutes read.

Python reversed() Function

Python reversed() Function The reversed() in Python returns a reverse iterator. Syntax reversed(seq) Parameter seq: This parameter represents any iterable object. Return This function returns a reversed iterator object. Example 1 # Python Program describing # the reversed() function ...

1 minute read.

Alexa Python

The cloud-based voice assistant renders text into speech in a female voice. Alexa is a virtual assistant released by Amazon. Using itself as a system for home automation, Alexa can...

4 minutes read.

Tensorflow Angular in Python

TensorFlow is an open-source, Python-compatible toolkit for numerical computation that accelerates and simplifies the creation of neural networks and machine learning algorithms. They make the interesting notion that models can be...

6 minutes read.

Python File Handling

What is the file? The file is a collection of data in a single unit. It is used to store information in the non-volatile memory such as hard-disk. Computer programs generally run on the RAM...

7 minutes read.

Python String

Python String Python string is considered as the most popular data type present in Python language. In Python, string data type is the collection of characters, letters, white spaces etc. written...

31 minutes read.

Python Permutations and Combinations

In mathematics, we all studied what is meant by permutations and combinations. “Permutations” define the way of arranging the elements in sequential order. “Combinations” mean the way of selecting the...

7 minutes read.

Loan Calculator using PyQt5 in Python

In the following tutorial, we will learn how to build a Loan Calculator application using the PyQt5 library in the Python programming language. So, let's get started. Introduction to the code: The heading...

4 minutes read.

Convert uppercase to lowercase in Python

Python String lower() By using the built-in string lower() method, all the uppercase characters can be converted into lowercase characters in a string, The lowercased string from the given string is returned...

1 minute read.

Sort Dictionary in Python

Dictionaries In Python, a dictionary is an unordered collection or set of data types that enable of store data in an unordered key-value/pair. The key is stored alongside with value. Dictionary contains key:...

4 minutes read.

Python program to find the GCD or HCF

Python program to find the GCD or HCF The largest positive integer that perfectly divides the two numbers is the HCF (Highest Common Factor) or GCD (Greatest Common Deviser). For example,...

5 minutes read.

Python Web Development projects

What is Python? Python is currently one of the most widely used computer programming languages. This isn't just an exaggeration; Python is the second-most famous programming language on the software development...

3 minutes read.

How To Install Python In Ubuntu

How To Install Python In Ubuntu Ubuntu is free and open-source software and it is an essential part of the Linux distribution. It is a popular operating system developed by Canonical. If we...

3 minutes read.

Python object()

Python object() class The object class in Python is a base for all classes. It returns a new featureless object. Syntax class object Parameter NA Return It returns a new featureless object. Example 1 # Python program explaining # the object()...

1 minute read.