×

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

Python MySQL Update Operation

Python MySQL Update Operation: In this part of tutorial, we will learn that how can we update a table present in SQL database through our Python program. As like SQL,...

4 minutes read.

Python setattr() function

Python setattr() function The setattr() function sets the value of the specified attribute of the specified object. Syntax setattr(object, name, value) Parameter object: This parameter represents any object. name: This parameter represents the name of the attribute you...

1 minute read.

Python zip() Function

Python zip() Function The zip() function makes an iterator that aggregates elements from each of the iterables and returns an iterator of tuples. Syntax zip(*iterables) Parameter iterables: Iterator objects that will be joined together Return This function...

1 minute read.

Palindrome program in Python

Palindrome program in python A number or string is said to be a palindrome if we invert the number or string and the string or number remains the same as the...

3 minutes read.

Python any() Function

Python any() Function The any() function in Python returns a boolean value ‘True’ if any element of the iterable is true or if the iterable is empty, else it returns False. Syntax any(iterable) Parameter Iterable: An iterable object (list, tuple, dictionary) Return This...

1 minute read.

Python Decorator

Python Decorator: A Decorator is an interesting feature of Python that helps the user design patterns and insert a new functionality to an existing object without making any modifications in...

6 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.

Pltpcolor in Python

Python: 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 is said...

3 minutes read.

How to get Time in seconds in Python

Python's time module source shows that are based on time. The time() method is used by developers and returns the current time as a UNIX timestamp. A UNIX timestamp is...

4 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.

Python Variable Scope with Local & Non-local Examples

This article will examine Python's global, local and non-local variables and show you how to use them to write code without problems. Let's quickly review what a variable in Python is...

8 minutes read.

Python Dictionary copy() method

Python Dictionary copy() method The dictionary.copy () method in Python returns a copy of the specified dictionary. Syntax dictionary.copy () Parameter NA Return None Example 1 # Python program explaining # the dictionary.copy() method # initialising the dictionary ...

1 minute read.

Python print() function

Python print() function The print() function prints the specified message to the screen or other standard output devices. Syntax print(*objects,sep=' ',end='\n',file=sys.stdout,flush=False) Parameter objects: This function represents an object, which will be converted to a string...

1 minute read.

Python Program to Find the gcd of Two Numbers

Introduction Greatest Common Divisor is the full form of gcd. The greatest common divisor, or GCD, of two numbers, is a value that can exactly divide the two digits and is...

5 minutes read.

Python Linear regression

In this tutorial, we will understand the meaning, usage, and types of Linear Regression. Further, we will comprehend the terms cost function and Optimization. Linear Regression is the widely used Machine...

3 minutes read.

Falcon Python

Introduction of Python Python is the fastest and the smartest programming language and it is an object oriented language. Python has libraries that can be importedeasily and perform many operations; to...

3 minutes read.

How to Open a file in python with Path

In this tutorial, we will see rather than the traditional way of opening a file by locating or navigating it from our desktops; we will learn how to open the...

2 minutes read.

Returning Multiple Values in Python

Python is considered a general-purpose programming language; it is a high-level programming language that is not much difficult and easier to learn. It is rich in libraries that can be...

3 minutes read.

Google Chrome API 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....

3 minutes read.

Python program to print array element present at even position

Python program to print array element present at even position In this program, we'll see a Python program that prints the elements of an array that are in even positions. We...

1 minute read.