Python string methods:
- Before going to string methods first of all we have to know what is meant by string.
- A "String is a set of or group of characters."
- The string always must be closed with single, double, or triple codes.
- Triple codes (''' ''') are used for multi-line purposes.
- In python, we are having some string functions, that are used to operate on strings.
- As the string is immutable it always returns the new values only. The old values cannot be replaced.
- The string function is defined as str().
Example:
#1.write python code using the str() methods.
- s'"Hello"
- print(s[0])
- print(s[1])
- print(s[2])
- print(s[3])
- print(s[4])
output: H
e
l
l
o
#2. write a python code using str() functions.
- str="Hello"
- print(str.lower())
- print(str.upper())
- print(str.count(str))
output: hello
HELLO
1
- In the following table, we will find more functions and their respective actions in the string.
| Method | Description |
|---|---|
| len() | used to determine the length of the string. |
| isalnum() | used to define the string is alphanumeric. |
| islower() | To define a string is in small letters. |
| isupper() | To define a string is in capital letters. |
| isnumeric() | The string consists of only numbers. |
| istitle() | The string is in the title case. |
| isspace() | The string has only white space. |
| find() | Searches the string value and returns the position. |
| format() | Formats specified values in a string |
| format_map() | Formats specified values in a string |
| index() | returns the position of string where it was found |
| count() | Returns the number of times string occurs. |
| stratswith() | the string starts with the given prefix. |
| isdecimal() | all characters in the string are decimals |
| isdigit() | all characters in the string are digits |
| isidentifier() | Returns True if the string is an identifier |
| endswith() | all characters in the string end with the given suffix. |
| encode() | it encodes the string. |
| isprintable() | all characters in the string are printable. |
| capitalize() | it returns all the capital strings. |
| center() | Returns the center string. |
| _contain_() | the string we check is contained in another string or not. |
| join() | Joins the elements of the string. |
| ljust() | Returns a left-justified version of the string |
| hash() | Returns the hash value of the given object. |
| lstrip() | Returns a left trim version of the string |
| maketrans() | Returns a translation table to be used in translations |
| partition() | Returns a tuple where the string is parted into three parts |
| replace() | string where a value is replaced with another value. |
| rfind() | Search the string and returns the last position. |
| rindex() | Search the string and returns the last position. |
| rjust() | Returns a right-justified version of the string |
| rpartition() | Returns a tuple where the string is parted into three parts |
| rsplit() | Splits the string at the specified separator, and returns a list |
| rstrip() | Returns a right trim of the string |
| split() | Splits the string at the specified separator, and returns a list |
| splitlines() | Splits the string at line breaks and returns a list |
| id() | Returns the identity of the string. |
| strip() | It trims the space between the strings. |
| swap case() | In this, the lower case becomes the upper case and vice versa |
| title() | Converts the first character of each word to upper case |
| translate() | Returns a translated string |
| upper() | Converts a string into the upper case. |
| zfill() | Fills string with a specified number of 0's at the beginning. |


No comments