{"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> 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 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 Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Similarly, check it out for the other tabsize.<\/p>\n Output:<\/strong><\/p>\n Output:<\/strong><\/p>\n Go through our tutorial and learn about various Python String Method Examples<\/a> and learn how to apply the knowledge while dealing with strings.<\/p>\n In the previous article, we have discussed Python String endswith() Method with Examples String expandtabs() Method in Python: The expandtabs() method expands(sets) the tabsize to the number of whitespaces specified. 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 …<\/p>\n
\nString expandtabs() Method in Python:<\/strong><\/p>\nstring.expandtabs(tabsize)<\/pre>\n
Given String = \"hello\\t this is\\t btechgeeks\\t\"<\/pre>\n
The string after setting to the default tabsize(8):\r\nhello this is btechgeeks<\/pre>\n
Given String = \"good\\t morning\\t btechgeeks\\t\"\r\nGiven tabsize = 5<\/pre>\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
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
1)Without Giving Tabsize<\/h5>\n
\n
# Give the string as static input and store it in a variable.\r\ngvn_str = \"hello\\t this is\\t btechgeeks\\t\"\r\n# Apply expandtabs() method to the given string that expands(sets) the tabsize\r\n# to the number of whitespaces specified. The default tabsize is eight(8).\r\n# Store it in another variable.\r\nrslt = gvn_str.expandtabs()\r\n# Print the string after setting to the default tabsize(8).\r\nprint(\"The string after setting to the default tabsize(8):\")\r\nprint(rslt)\r\n<\/pre>\n
The string after setting to the default tabsize(8):\r\nhello this is btechgeeks<\/pre>\n
2)With Giving Tabsize<\/h5>\n
\n
# Give the string as static input and store it in a variable.\r\ngvn_str = \"good\\t morning\\t btechgeeks\\t\"\r\n# Give the tabsize as static input and store it in another variable.\r\ngvntab_size = 5\r\n# Apply expandtabs() method to the given string by passing the given tabsize\r\n# as an argument that expands(sets) the tabsize to the given number of\r\n# whitespaces specified. The default tabsize is eight(8).\r\n# Store it in another variable.\r\nrslt = gvn_str.expandtabs(gvntab_size)\r\n# Print the string after setting it to the given number of whitespaces\r\n# specified(tabsize).\r\nprint(\"The string after setting to the given tabsize{\", gvntab_size, \"} is :\")\r\nprint(rslt)\r\n<\/pre>\n
The string after setting to the given tabsize{ 5 } is :\r\ngood morning btechgeeks<\/pre>\n
# Similarly, check it out for the other tabsize.\r\ngvn_str = \"good\\t morning\\t btechgeeks\\t\"\r\ngvntab_size = 10\r\nrslt = gvn_str.expandtabs(gvntab_size)\r\nprint(\"The string after setting to the given tabsize{\", gvntab_size, \"} is :\")\r\nprint(rslt)\r\n<\/pre>\n
The string after setting to the given tabsize{ 10 } is :\r\ngood morning btechgeeks<\/pre>\n
# Similarly, check it out for the other tabsize.\r\ngvn_str = \"good\\t morning\\t btechgeeks\\t\"\r\ngvntab_size = 1\r\nrslt = gvn_str.expandtabs(gvntab_size)\r\nprint(\"The string after setting to the given tabsize{\", gvntab_size, \"} is :\")\r\nprint(rslt)\r\n<\/pre>\n
The string after setting to the given tabsize{ 1 } is :\r\ngood morning btechgeeks<\/pre>\n
\n