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

In This Article You Will Learn About 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)
python-string-method
As it is shown clearly that tab size of string has increased.

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)
python-string-method
As it is shown clearly that tab size of string has increased.

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)
python-string-method
As it is shown clearly that letter ‘l’ is presented in string therefore it returned its index number 2.

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)
python-string-method
As it is shown clearly that letter ‘b’ is not present in string therefore it returned -1.

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

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

print(x)
python-string-method
As it is shown clearly that letter ‘l’ is found between the range.

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)
python-string-method
As it is shown clearly that index() method returned a ValueError because it did not find letter ‘K’ in string.

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)
python-string-method
As it is shown clearly that format method contains two arguments that will be filled to placeholder as per sequence because there is no value defined inside placeholder.

Example 2- Use of placeholder with positional argument.

x = 'Hello, {0}, in {1} world.' .format('welcome', 'Python')
print(x)
python-string-method
As it is shown clearly that format method contains two arguments that will be filled to placeholder as per value defined inside placeholder.

Example 3- Use of placeholder with keyword argument.

x = 'Hello, {greet}, in {course}, world.' .format(greet = 'welcome', course = 'Python')
print(x)
python-string-method
As it is shown clearly that placeholder value is taken from key=value argument given inside format method.

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)
python-string-method
As it is shown clearly that format() method contains combination of argument that will be filled to placeholder according to value. Number 0 belongs to ‘Python’ and greet belongs to ‘welcome’.

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()”

    • 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.

      Reply

Leave a Comment

Stay in the loop

codingstreets