codingstreets
Search
Close this search box.
python-string-methods-expandtabs-find-and-format

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

In this article, learn how to improve text alignment in Python with expandtabs(). Master efficient string searching in Python using find(). Unlock the power of dynamic string formatting with Python’s format().

Before moving ahead, let’s take a look at Python String Methods.

Table of Contents

expandtabs() method

expandtabs() returns the replaced text of the original text after adding the tab size between characters.

Syntax – string.expandtabs(tab size)

Parameter value –

tab size – Default tab size is 8, otherwise it specified number of tab size in characters.

Example: Set the tab size of 5 and 12.

				
					text = 'p\ty\tt\th\to\tn'    # insert 't' before letter to add tab size
tab_5 = text.expandtabs(5)  
tab_12 = text.expandtabs(12)

print("default tab size 8:",text)
print("after tab size 5:",tab_5)
print("after tab size 12:",tab_12)

#output:

default tab size 8: p	y	t	h	o	n
after tab size 5: p    y    t    h    o    n
after tab size 12: p           y           t           h           o           n
​

				
			

Example: Set the tab size of 3 and 6.

				
					original_text = "Python World"
text = 'p\ty\tt\th\to\tn \tW\to\tr\tl\td'    # insert 't' before letter to add tab size
tab_3 = text.expandtabs(3)  
tab_6 = text.expandtabs(6)

print("original_text:",original_text)
print("after tab size 5:",tab_3)
print("after tab size 12:",tab_6)

#output:

original_text: Python World
after tab size 5: p  y  t  h  o  n  W  o  r  l  d
after tab size 12: p     y     t     h     o     n     W     o     r     l     d
				
			

find()method

find() retutrns the index number of first occurrence of a substring within a string, if not found returns -1.

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

Parameter value –

value – Required for searching sub-string index number.

start – Optional argument, used from where to start search.

end – Optional argument, used from where to end search.

Example: Character found. Return the index number of first occurrence of the letter “l”.

				
					text = 'Hello!'
text_index = text.find('l') 

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

#output:
The index number of 'l' is: 2
				
			

Example: Character not found. Return the index number of first occurrence of the letter “A”.

				
					text = 'Hello!'
text_index = text.find('A') 

print("Index number of 'A' not found is:", text_index)

#output:
Index number of 'A' not found is: -1
				
			

Example: Returns the index number using the optional arguments.

				
					text = 'Hello!'
text_index = text.find('l', 0, 3) 

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

#output:
The index number of 'l' is: 2
				
			

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: If character not found, index() method returns value error.

				
					text = 'Hello! World'
text_index = text.index('A') 

print("The index number of 'A' is:", text_index)

#output:
ValueError: substring not found
				
			

format() method

format() inserts the specific value in the placeholders in given the string.

Placeholder – Placeholder is 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 – Required value that 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: Use of placeholder as empty or default.

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

print(text)


#output:
Hello, welcome in Python world.
				
			

Explanation: format() method contains two arguments that will be filled to placeholder as per sequence because there is no value defined inside placeholder.

Example: Use of placeholder with positional argument.

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

print(text)

#output:
Hello, welcome in Python world.
				
			

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

				
					text = 'Hello, {0} in {course} world.'.format('welcome', course='Python')

print(text)

#output:
Hello, Python in welcome world.
				
			

Conclusion

By understanding and utilizing these Python string methods, you can significantly enhance your string manipulation capabilities. Whether it’s adjusting tab spacing, searching for specific substrings, or formatting strings dynamically, these methods offer valuable functionality to make your code more efficient and expressive.

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on