# Python String Functions

0
831

Hey! Everyone I hope y’all are doing great in your life. so today we will be talking about built-in Python String Functions.

## What is a Function?

A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing. The good thing about functions is that they are famous with several names. Different programming languages name them differently, for example, functions, methods, subroutines,  procedures, etc. If you come across any such terminology, then just imagine the same concept, which we are going to discuss.

Built-in Functions For String in Python

Python has a set of built-in methods that you can use on strings. All string methods return new values. They do not change the original string.

The methods are as follows:

## String capitalize()

The capitalize() function/method returns a string where the first character is upper case.

Example:

txt = "hello, how are you?"
a = txt.capitalize()
print(a)
Result: Hello, how are you.

## casefold()

The casefold() function/method returns a string where all the characters are lower case.

Example:

txt = "Hello, How Are You?"
a = txt.casefold()
print(a)
Result: hello, how are you

## center()

The center() function/method will center align the string, using a specified character (space is the default) as the fill character.

Syntax:
string.center(length, character)

Length is required because it will tell the interpreter to which length the string should be aligned in the center.
Character is optional. if you want to add some character on both sides you can write one or else it will add space by default.

Example:

txt = "banana"
x = txt.center(20, "O")
print(x)
Result:0000000banana0000000

OR

txt = "banana"
x = txt.center(20)
print(x)
Result:       banana

Also Read: Python Function Part 2

## count()

The count() function/method returns the number of times a specified value appears in the string.

Syntax:
string.count(value, start, end)

Value is required because it’s the string value that is to be searched from the main string.
The start is the position or index value from where the search should start. By default its 0
The end is the position or index value where the search ends. By default, it’s the end of the whole string.

Example:

txt = "I like pianos, piano is my fav instrument"
a = txt.count("piano", 10, 24)
print(a)
Result:1

OR

txt = "I like pianos, piano is my fav instrument"
a = txt.count("piano")
print(a)
Result:2

## encode()

The encode() function/method encodes the string, using the specified encoding. If no encoding is specified, UTF-8 will be used.

Syntax:
string.encode(encoding=encoding, errors=errors)

Encoding is a string used to specify the encoding to be used. The default is UTF-8 errors A string is used for specifying the error method.

Legal values are:

‘backslashreplace’ – uses a backslash instead of the character that could not be encoded.
‘ignore’ – ignores the characters that cannot be encoded.
‘namereplace’- replaces the character with a text explaining the character.
‘strict’- raises an error on failure.
‘replace’- replaces the character with a question mark.
‘xmlcharrefreplace’- replaces the character with an XML character.

Example:

txt = "My name is Tåbinda"

print(txt.encode(encoding="ascii",errors="backslashreplace"))
print(txt.encode(encoding="ascii",errors="ignore"))
print(txt.encode(encoding="ascii",errors="namereplace"))
print(txt.encode(encoding="ascii",errors="replace"))
print(txt.encode(encoding="ascii",errors="xmlcharrefreplace"))
print(txt.encode(encoding="ascii",errors="strict"))
Result:
b'My name is T\\xe5binda'
b'My name is Tbinda'
b'My name is T\\N{LATIN SMALL LETTER A WITH RING ABOVE}binda'
b'My name is T?binda'
b'My name is Tåbinda'

## endswith()

The endswith() function/method returns True if the string ends with the specified value, otherwise, it returns  False.

Syntax:
string.endswith(value, start, end)

Value is required to check if the value ends with that value or not.

Start it specifies at which position or index to start the search.
End it specifies at which position or index to end the search.

Example:

txt = "hey there."
a = txt.endswith(".")
print(a)
Result:True

OR

txt = "Hello, welcome to my world."
a = txt.endswith("my world.", 8, 14)
print(a)
Result: False

## expandtabs()

The expandtabs() function/method sets the tab size to the specified number of whitespaces.

Syntax:
string.expandtabs(tabsize)

Tab size is a number specifying the tab size. By default, the tab size is 8.

Example:

txt = "H\te\tl\tl\to"

print(txt)
print(txt.expandtabs())
print(txt.expandtabs(2))
print(txt.expandtabs(4))
print(txt.expandtabs(10))
Result:

H       e       l       l       o
H       e       l       l       o
H e l l o
H   e   l   l   o
H         e         l         l         o

## find()

The find() function/method finds the first occurrence of the specified value. It returns -1 if the value is not found. The find() function/method is almost the same as the index() function/method, the only difference is that the index() function/method raises an exception if the value is not found.

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

Value is the value to be searched for.
The start is used to tell where to start the search from. The default is 0.
The end is used to tell where to start the search.

Example:

txt = "Hello, nice to meet you."
x = txt.find("e")
print(x)
Result:1

OR

txt = "Hello, welcome to my world."
x = txt.find("e", 5, 10)
print(x)
Result:8

OR

txt = "Hello, nice to meet you."
print(txt.find("q"))
print(txt.index("q"))
Result:

-1
Traceback (most recent call last):
File "./prog.py", line 4, in <module>
ValueError: substring not found

## format()

The format() is the method that returns the formatted string. It formats the specified value(s) and inserts them inside the string’s placeholder. The placeholder is defined using curly brackets: {}.

Syntax:
string.format(value1, value2…)

One or more values should be formatted and inserted in the string. The values can be A number specifying the position of the element you want to remove.
The values are either a list of values separated by commas, a key=value list, or a combination of both.
The values can be of any data type

NOTE: The placeholders can be identified using named indexes {price}, numbered indexes {0}, or even empty placeholders {}.

Example:

Insert the price inside the placeholder, the price should be in fixed point, two-decimal format.

txt = "For only {price:.2f} dollars!"
print(txt.format(price = 100))
Result: For only 100.00 dollars!

OR

#named indexes:
txt1 = "My name is {fname}, I'm {age}".format(fname = "anna", age = 26)
#numbered indexes:
txt2 = "My name is {0}, I'm {1}".format("anna",26)
#empty placeholders:
txt3 = "My name is {}, I'm {}".format("anna",26)

print(txt1)
print(txt2)
print(txt3)
Result:

My name is anna, I'm 26
My name is anna, I'm 26
My name is anna, I'm 26

OR

#To demonstrate, we insert the number 8 to set the available space for the value to 8 characters.
#Use "<" to left-align the value:
txt = "We have {:<8} chickens."
print(txt.format(75))
Result: We have 75       chickens.

OR

#To demonstrate, we insert the number 8 to set the available space for the value to 8 characters.
#Use "^" to center-align the value:
txt = "We have {:^8} chickens."
print(txt.format(75))
Result: We have    75    chickens.

OR

#To demonstrate, we insert the number 8 to set the available space for the value to 8 characters.
#Use ">" to right-align the value:
txt = "We have {:>8} chickens."
print(txt.format(75))
Result: We have       75 chickens.

OR

#To demonstrate, we insert the number 8 to specify the available space for the value.
#Use "=" to place the plus/minus sign at the left most position:
txt = "The temperature is {:=8} degrees celsius."
print(txt.format(-5))
Result: The temperature is -      5 degrees.

OR

#Use "+" to always indicate if the number is positive or negative:
txt = "The temperature is between {:+} and {:+} degrees celsius."
print(txt.format(-3, 7))
Result: The temperature is between -3 and +7 degrees celsius.

OR

#Use "-" to always indicate if the number is negative (positive numbers are displayed without any sign):
txt = "The temperature is between {:-} and {:-} degrees celsius."
print(txt.format(-3, 7))
Result: The temperature is between -3 and 7 degrees celsius.

OR

#Use " " (a space) to insert a space before positive numbers and a minus sign before negative numbers:
txt = "The temperature is between {: } and {: } degrees celsius."
print(txt.format(-3, 7))
Result: The temperature is between -3 and  7 degrees celsius.

OR

#Use "," to add a comma as a thousand separator:
txt = "The universe is {:,} years old."
print(txt.format(13800000000))
Result: The universe is 13,800,000,000 years old.

OR

#Use "_" to add a underscore character as a thousand separator:
txt = "The universe is {:_} years old."
print(txt.format(13800000000))
Result: The universe is 13_800_000_000 years old.

OR

#Use "b" to convert the number into binary format:
txt = "The binary version of {0} is {0:b}"
print(txt.format(5))
Result: The binary version of 5 is 101

OR

c Converts the value into the corresponding Unicode character.

OR

#Use "d" to convert a number, in this case a binary number, into decimal number format:
txt = "We have {:d} chickens."
print(txt.format(0b101))
Result: We have 5 chickens.

OR

#Use "e" to convert a number into scientific number format (with a lower-case e):
txt = "We have {:e} chickens."
print(txt.format(5))
Result: We have 5.000000e+00.

OR

#Use "E" to convert a number into scientific number format (with an upper-case E):
txt = "We have {:E} chickens."
print(txt.format(5))
Result: We have 5.000000E+00.

OR

#Use "f" to convert a number into a fixed point number, default with 6 decimals, but use a period followed by a number to specify the number of decimals:
txt = "The price is {:.2f} dollars."
print(txt.format(45))
#without the ".2" inside the placeholder, this number will be displayed like this:
txt = "The price is {:f} dollars."
print(txt.format(45))
Result: The price is 45.00 dollars.
The price is 45.000000 dollars.

OR

#Use "F" to convert a number into a fixed point number, but display inf and nan as INF and NAN:
x = float('inf')
txt = "The price is {:F} dollars."
print(txt.format(x))
#same example, but with a lower case f:
txt = "The price is {:f} dollars."
print(txt.format(x))
Result:

The price is INF dollars.
The price is inf dollars

OR

#Use "o" to convert the number into octal format:
txt = "The octal version of {0} is {0:o}"
print(txt.format(10))
Result: The octal version of 10 is 12

OR

#Use "x" to convert the number into Hex format:
txt = "The Hexadecimal version of {0} is {0:x}"
print(txt.format(255))
Result: The Hexadecimal version of 255 is ff

OR

#Use "X" to convert the number into upper-case Hex format:
txt = "The Hexadecimal version of {0} is {0:X}"
print(txt.format(255))
Result: The Hexadecimal version of 255 is FF

OR

#Use "%" to convert the number into a percentage format:
txt = "You scored {:%}"
print(txt.format(0.25))
#Or, without any decimals:
txt = "You scored {:.0%}"
print(txt.format(0.25))
Result: You scored 25.000000%
You scored 25%

some other format types are :

:g General formal
:G General format(using an upper case E for scientific notations)
:n Number format

So these are some string we shall cover some more of it in the next article. I hope you find this article helpful. If you have any questions use the comment section and feel free to leave some suggestions too.