{"id":24774,"date":"2021-11-02T09:46:19","date_gmt":"2021-11-02T04:16:19","guid":{"rendered":"https:\/\/python-programs.com\/?p=24774"},"modified":"2021-11-05T19:16:23","modified_gmt":"2021-11-05T13:46:23","slug":"python-string-expandtabs-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-string-expandtabs-method-with-examples\/","title":{"rendered":"Python String expandtabs() Method with Examples"},"content":{"rendered":"

In the previous article, we have discussed Python String endswith() Method with Examples<\/a>
\nString expandtabs() Method in Python:<\/strong><\/p>\n

The expandtabs() method expands(sets) the tabsize to the number of whitespaces specified.<\/p>\n

The expandtabs() method returns a string copy with all tab characters ‘\\t’ replaced with whitespace characters until the next multiple of the tabsize parameter is reached.<\/p>\n

Syntax:<\/strong><\/p>\n

string.expandtabs(tabsize)<\/pre>\n

Parameters<\/strong><\/p>\n

tabsize:<\/strong> This is Optional. It is a number that specifies the tabsize. The default tabsize is eight(8).<\/p>\n

Return Value:<\/strong><\/p>\n

expandtabs() returns a string in which all ‘\\t’ characters are replaced with whitespace until the next multiple of the tabsize parameter is reached.<\/p>\n

Examples:<\/strong><\/p>\n

Example1:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given String = \"hello\\t this is\\t btechgeeks\\t\"<\/pre>\n

Output:<\/strong><\/p>\n

The string after setting to the default tabsize(8):\r\nhello    this is         btechgeeks<\/pre>\n

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given String = \"good\\t morning\\t btechgeeks\\t\"\r\nGiven tabsize = 5<\/pre>\n

Output:<\/strong><\/p>\n

The string after setting to the given tabsize{ 5 } is :\r\ngood  morning   btechgeeks<\/pre>\n

String expandtabs() Method with Examples in Python<\/h2>\n