# Python add or insert tab (\\t) into list

**URL:** <https://discuss.python.org/t/python-add-or-insert-tab-t-into-list/27541>\
**Category:** Python Help\
**Tags:** help\
**Created:** [June 8, 2023, 1:07pm UTC](https://discuss.python.org/t/python-add-or-insert-tab-t-into-list/27541 "2023-06-08T13:07:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![PraveenJaikumar](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/praveenjaikumar/32/12821_2.png) [@PraveenJaikumar](https://discuss.python.org/u/PraveenJaikumar)\
**Post date:** [June 8, 2023, 1:07pm UTC](https://discuss.python.org/t/python-add-or-insert-tab-t-into-list/27541/1 "2023-06-08T13:07:51Z")

</div>

Hi All,  
I am new to python. I am trying to add a /t to a string variable and insert in to a list.  
when I print the string, it prints with tab space.  
When the same variable added to the list it prints \t. how to fix it.

```auto
str1="new_service_group"
src_grp="servicegroups\t\t\t\t"+str1
print("src_grp is = "+src_grp)
array=['define service{', 'host_name host-a1p', 'service_description Disk Space /var', 'use ch_rating_system', '}']
array.insert(len(array)-1,src_grp)
print(array)

```

output:

```auto
src_grp is = servicegroups new_service_group
['define service{', 'host_name host-a1p', 'service_description Disk Space /var', 'use ch_rating_system', 'servicegroups\t\t\t\tnew_service_group', '}']

```

---

<div class="post-metadata">

**Author:** ![rob42](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rob42/32/8218_2.png) [@rob42](https://discuss.python.org/u/rob42)\
**Post date:** [June 8, 2023, 1:21pm UTC](https://discuss.python.org/t/python-add-or-insert-tab-t-into-list/27541/2 "2023-06-08T13:21:35Z")

</div>

My advice (FWIW) is not to do that: if you want to present the contents of a list object, formatted in a particular way (be that with tab spaces, or whatever) then introduce the ‘formatting’ when you construct the output, rather then mixing the formatting with the data.

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [June 8, 2023, 1:23pm UTC](https://discuss.python.org/t/python-add-or-insert-tab-t-into-list/27541/3 "2023-06-08T13:23:23Z")

</div>

Hi! Welcome to the Discourse and to Python programming!

What you’re seeing here is the difference between the _string_ form and the _representation_. Inside a list, you’re seeing the representation (or “repr”) of the strings, which is designed to be similar to source code; it’s also meant to be unambiguous (you can’t confuse tabs and spaces when the tabs are shown as `"\t"`).

So what you want here is to print out a series of things. That’s awesome, Python can do that for you! Here’s one way:

```auto
for line in array:
    print(line)

```

Or alternatively, you can join the parts together. Python has a slightly quirky way of spelling this, but there are good reasons for that:

```auto
service_config = " ".join(array)
print(service_config)

```

Give those two a try and see what you think of them. You may find that they’re not precisely what you want, but you can tweak from there.

Have fun with it!

---

<div class="post-metadata">

**Author:** ![aivarpaalberg](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/aivarpaalberg/32/9457_2.png) [@aivarpaalberg](https://discuss.python.org/u/aivarpaalberg)\
**Post date:** [June 8, 2023, 1:49pm UTC](https://discuss.python.org/t/python-add-or-insert-tab-t-into-list/27541/4 "2023-06-08T13:49:16Z")

</div>

> [@PraveenJaikumar](#):
>
> ```auto
> src_grp="servicegroups\t\t\t\t"+str1
> print("src_grp is = "+src_grp)
> 
> ```

To output object value along with its name one can use `f-strings support a handy = specifier for debugging` (introduced in [Python 3.8](https://www.python.org/downloads/release/python-380/)):

```auto
>>> spam = "bacon and eggs"
>>> print(f"{spam=}")
spam='bacon and eggs'
>>> spam = 6
>>> print(f"{spam=}")
spam=6

```

---

<div class="post-metadata">

**Author:** ![PraveenJaikumar](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/praveenjaikumar/32/12821_2.png) [@PraveenJaikumar](https://discuss.python.org/u/PraveenJaikumar)\
**Post date:** [June 8, 2023, 2:18pm UTC](https://discuss.python.org/t/python-add-or-insert-tab-t-into-list/27541/5 "2023-06-08T14:18:52Z")

</div>

Hi All,  
Thanks for your replies. I got it resolved by using expandtabs()

```auto
str1="new_service_group"
src_grp="servicegroups\t\t\t\t"+str1
print("src_grp is = "+src_grp)
array=['define service{', 'host_name host-a1p', 'service_description Disk Space /var', 'use ch_rating_system', '}']
array.insert(len(array)-1,src_grp.expandtabs())
print(array)

```

I’ll try to format the values when I print it.
