0% found this document useful (0 votes)
5 views15 pages

String Function

Uploaded by

2022031172
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views15 pages

String Function

Uploaded by

2022031172
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Python String Functions

The following are the python string function mentioned:

1. capitalize(): Converts the initial letter of the string to uppercase.

Example:

Code:

str1 = “hello from MMMUT”

str2 = str1.capitalize()
print(str2)

Output:

Hello From MMMUT

2. casefold(): Converts the entire string to lowercase.

Example:

Code:

str1 = “HELLO FROM MMMUT”

str2 = str1.casefold()
print(str2)
Output:

hello from MMMUT

3. center(): It aligns the string at the center of the specified length.

Example:

Code:

str1 = “MMMUT”

str2 = str1.center(10)
print(str2)

Output:

MMMUT

That is, two-character spaces on the left have left a void, followed by six

characters of the string and another two void characters on the right,

summing to a total of ten characters.

4. count(): Returns the number of times a substring occurs in the given

string.
Example:

Code:

str1 = “Hello from MMMUT. Welcomw to MMMUT”

num = str1.count(“MMMUT”)
print(str2)

Output:

5. encode(): Converts the string into its encoded version.

Example:

Code:

str1 = “MMMUT”

str2 = str1.encode()
print(“Hello from”, str2)

Output:

Hello from vfg/7hyt/4


6. endswith(): Returns true if the given string ends with the specified

substring.

Example:

Code:

str1 = “Hello from MMMUT”

str2 = str1.endswith(“CBA”)
print(str2)

Output:

true

7. expandtabs(): Replaces the tab size to the given numeric character

spaces. The default tab size is 8 character spaces.

Example:

Code:

str1 = “Hello\tfrom\tMMMUT”

str2 = str1.expandtabs(2)
print(str2)
Output:

Hello from MMMUT

8. find(): Searches the main string from the left for a specified substring

and returns its position within a match is found; if not, return -1 when no

match is found.

Example:

Code:

str1 = “Hello from MMMUT”

str2 = str1.find(“MMMUT”)
print(str2)

Output:

11

9. format(): Helps format the string by making use of placeholders.

Example:

Code:
str1 = “MMMUT”
print("Hello from {}.".format(str1))

Output:

Hello from MMMUT.

10. index(): Finds the position of occurrence of a substring by searching

the main string for a specified substring and returns its position within a

match is found, if not throws an error.

Example:

Code:

str1 = “Hello from MMMUT”

str2 = str1.index(“MMMUT”)
print(str2)

Output:

11
11. isalnum(): Determines if all the characters in a given string are

alphanumeric, that is, only alphabets and numbers. If yes, then returns true,

else returns false. In case there is a space in between, it returns false.

Example:

Code:

str1 = “MMMUT123”

str2 = str1.isalnum()
print(str2)

Output:

True

12. isalpha(): Determines if all the characters in the given string are

alphabets. If yes, return true, else return false. In case there is a space in

between, it returns false.

Example:

Code:

str1 = “HellofromMMMUT”
str2 = str1.isalpha()
print(str2)

Output:

True

13. isdecimal(): Determines if all the characters in a given string are

decimals. If yes, then returns true, else returns false. In case there is a space

in between, it returns false.

Example:

Code:

str1 = “123456”

str2 = str1.isdecimal()
print(str2)

Output:

True
14. isidentifier(): Determines whether or not the string is a valid identifier.

If yes, then returns true, else returns false. In case there is a space in

between, it returns false.

Example 1:

Code:

str1 = “MMMUT123”

str2 = str1.isidentifier()
print(str2)

Output:

True

Example 2:

Code:

str1 = “MMMUT 123”

str2 = str1.isidentifier()
print(str2)

Output:
False

15. islower(): Determines if all the characters in a given string are in lower

case. If yes, then returns true, else returns false.

Example:

Code:

str1 = “MMMUT”

str2 = str1.islower()
print(str2)

Output:

False

16. isnumeric(): Determines if all the characters in a given string are

numeric, that is, numbers and exponents that could be in fractions. If yes,

then returns true, else return false.

Example:

Code:
str1 = “123”

str2 = str1.isnumeric()
print(str2)

Output:

True

17. isprintable(): Determines if all the characters in a given string are

printable or not. If yes, then returns true, else returns false. Characters such

as “\t” or “\n” are not printable.

Example 1:

Code:

str1 = “MMMUT123”

str2 = str1.isprintable()
print(str2)

Output:

True

Example 2:
Code:

str1 = “\tMMMUT123”

str2 = str1.isprintable()
print(str2)

Output:

False

18. isspace(): Determines if all the characters in a given string are white

spaces. If yes, then returns true, else returns false.

Example:

Code:

str1 = “ “

str2 = str1.isspace ()
print(str2)

Output:

True
19. istitle(): Determines if a string follows a set of rules to qualify as a title.

If yes, then returns true, else returns false.

Example:

Code:

str1 = “Hello From MMMUT”

str2 = str1.istitle()
print(str2)

Output:

True

20. isupper(): Determines if all the characters in a given string are in the

upper case. If yes, then returns true, else returns false.

Example:

Code:

str1 = “HELLO FROM MMMUT”

str2 = str1.isupper()
print(str2)
Output:

True

21. join(): Meant to concatenate two strings in an iterated manner.

Example:

Code:

str1 = “Hello”

str2 = str1.join(“MMMUT”)
print(str2)

Output:

HelloEHellodHellouHelloCHelloBHelloA

22. lower(): Meant to convert the entire string to lower case.

Example:

Code:

str1 = “Hello from MMMUT.”

str2 = str1.lower()
print(str2)

Output:

hello from MMMUT.

23. upper(): Meant to convert the entire string to the upper case.

Example:

Code:

str1 = “Hello from MMMUT”

str2 = str1.upper()
print(str2) Output: HELLO FROM MMMUT 24. replace(): Meant to

replace a substring with another. Example: Code:

str1 = “Hello from MMMUT!”

str2 = str1.replace(“ from”,” there”)


print(str2)

Output:

Hello there MMMUT!

You might also like