```
table_name='Customer$'
if table_name.startswith('$'):
    table_name=table_name[1:]
if table_name.endswith('$'):
    table_name=table_name[:-1]

```

I tried with the above code it gives me correct result as

```
 Customer

```

Is there any optimized way of doing it? please reply
['https://Stackoverflow.com/questions/63154860', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/13818054/']
Use `.strip()`:

```
table_name = '$Customer$'.strip('$')

```

This will remove all `$`s from the start and end and do nothing if there aren't dollars surrounding the string.
just use `.strip()` it works like `trim()`

```
table_name = '$Cu$$stomer$'
ans = table_name.strip('$')
print(ans)  # output Cu$$stomer

```

The `strip()` method returns a copy of the string by removing both the leading and the trailing characters (based on the string argument passed).

you can learn more about `strip()` from here: <https://www.programiz.com/python-programming/methods/string/strip>