user9175260
user9175260

Reputation:

Replace tab between names with a space

How can I get rid of that tab with regular expressions? I also need to do it on the fly because the name will be inside a list.

import re
x = 'Muh    kay'

I want x to be

x = 'Muh Kay'

with only one space between them.

Upvotes: 2

Views: 844

Answers (3)

J Agustin Barrachina
J Agustin Barrachina

Reputation: 4090

I would do:

x.replace('\t', ' ')

However, this won't add the capital letter as the example shows.

Upvotes: 0

Prayson W. Daniel
Prayson W. Daniel

Reputation: 15568

This will do:

x = 'Muh    kay'   
x = ' '.join(i.capitalize() for i in x.split())

Upvotes: 1

NKR
NKR

Reputation: 186

You can use something like this re.sub(r'\t', ' ', s) which will replace tabs with space. Please refer this answer: How to replace custom tabs with spaces in a string, depend on the size of the tab?

Upvotes: 2

Related Questions