# Python String Method expandtabs(), find() and format()

Python String Method – Before moving ahead, let’s know a little bit about Python String Method Casefold(), Endswith() And Encode()

expandtabs() method – It returns replaced characters of original string after adding tab size between characters.

Syntax - string.expandtabs(tab size)

Parameter value -

tab size - By default it works as tab size 8 otherwise it specified number of tab size in characters.

Example 1- Set the tab size of 5 and 12.

x = 'p\ty\tt\th\to\tn'
z = x.expandtabs(5)
c = x.expandtabs(12)

print(x)
print(z)
print(c)

Example 2 – Set the tab size of 8 and 12.

original_string = 'Welcome'
x = 'W\te\tl\tc\to\tm\te'
z = x.expandtabs()
c = x.expandtabs(12)

print("original_string:", original_string)
print(z)
print(c)

find() method – It returns the index number of first appeared letter. If character not found it returns -1.

Syntax - string.find(value, start, end)

Parameter value -

value - It is required for searching sub-string index number.

start - It is optional argument. It is used from where to start search.

end - It is optional argument. It is used from where to end search.

Example 1- Character found. Using find() as default without any argument.

x = 'Hello!'
z = x.find('l')

print("The index number of 'l' is:", z)

Example 2- Character not found. Use of find() as default without any optional argument.

x = 'Hello!'
z = x.find('b')

print("The index number of 'b' is:", z)

Example 3- Use of find() with optional argument.

x = 'Hello!'
z = x.find('l', 0, 5)

print(x)

Note: The find() method is almost same as index() method but the only one difference is that if character doesn’t contain in given string, find() method shows -1 whereas index() method shows value error.

Example 4- If character not found, find() method shows -1 but index() method shows value error.

x = 'python in python'
z = x.find('K')
y = x.index('K')

print(z)
print(y)

format() method – It inserts specific value inside placeholders in the given string.

Placeholder – It defined as indexed of number {0}, indexed of name {name} and empty placeholder {} as well.

Syntax - string.format( value1, value2... )

Parameter value -

value1 or value2 - It is required value. Which specify one or more values that should be inserted in the given string.

The value can be any type of data type such as list of values separated by comma (,), list of key=value or a combination of both.

Example 1- Use of placeholder as empty or default.

x = 'Hello, {}, in {}, world.' .format('welcome', 'Python')
print(x)

Example 2- Use of placeholder with positional argument.

x = 'Hello, {0}, in {1} world.' .format('welcome', 'Python')
print(x)

Example 3- Use of placeholder with keyword argument.

x = 'Hello, {greet}, in {course}, world.' .format(greet = 'welcome', course = 'Python')
print(x)

Example 4- Use of placeholder with combinations of both (positional and keyword) arguments.

x = 'Hello, {0}, in {greet}, world.' .format('Python', greet = 'welcome')
print(x)

If you find anything incorrect in above discussed topic and have any further question, please comment down below.

Like us on

### 3 thoughts on “Python String Method expandtabs(), find() and format()”

1. I type:
X.
I expect to get the pop window that lists the extensions like x.uppercase or x.lowercase

But that doesn’t happen

• First of all, please make sure that text stored in variable X should be class string.

You got an error because the list object does not have the “upper” and “lower” case. In other words, you cannot change the case of a list of strings. To change the case of a string, store a single string in a variable, not a list of strings.

For example –

For Uppercase –
X = “Python”
print(X.upper())

For Lowercase –
X = “Python”
print(X.lower())

Let us know if we can help you with something else too. – CS Team.