Strings
Anything we write inside a single quotes or double quotes in python will be consider as strings.
It makes no difference either we write a string in single quotes or double quotes.
String Sciling
String Slicing is use to slice the certain part of a string. which we had already learned in the Slicing part. If you wish to revise the topic. Click on the below button.
String Addition
We can join two or more strings using the addition operator.
String Format
We can format string using the format method or f string
Multiline String
Using three double quotes or three single quotes we can create a multiline string.
Iterate String
You can iterate over a string using a for loop.
String Methods
String methods are use to modify and manipulate strings according to our need. We are going to discuss few of the important string methods.
upper()
lower()
replace()
Syntax : replace(old, new, count)
count is optional parameter. If count is not given then it will replace all the occurance.
index()
-
Index method will search it from the left side and return the index number of the first occurrence of the specified character.
-
It will throw an error if the specified character doesn't exists in a string.
rindex()
-
rindex method will search it from the right side and return the index number of the first occurrence of the specified character.
-
It will throw an error if the specified character doesn't exists in a string.
find()
-
find method will search it from the left side and return the index number of the first occurrence of the specified character.
-
It will not throw an error if the specified character doesn't exists in a string.
-
It will return -1 if the specified character is not present in a string.
rfind()
-
rfind method will search it from the right side and return the index number of the first occurrence of the specified character.
-
It will not throw an error if the specified character doesn't exists in a string.
-
It will return -1 if the specified character is not present in a string.
capitalize()
split()
-
Syntax : split(separator,no_of_splits)
-
Both the parameters are optional. If we don't pass the values then separator will be whitespace and all the occurrence will be splitted into a list.
strip()
rstrip()
lstrip()
swapcase()
title()
count()
-
Syntax : count(character,start,stop)
-
Both the parameters start and stop are optional. If we don't pass the values then the complete string is considered for counting the number of specific character.
join()
-
Syntax : 'separator'.join(iterable)
-
With the help of a join method, we can join the string from an iterable object.
startswith()
endswith()
isalpha()
isnumeric()
isalnum()
islower()
isupper()
String Methods
Method | Explanation |
---|---|
lower() | Convert all the character to lowercase |
upper() | Convert all the character to uppercase. |
swapcase() | Convert all the lowercase character to uppercase character and vice versa |
replace() | replace the characters in a string. |
index() | return the index number of the first occurrence of the character from left side. |
rindex() | return the index number of the first occurrence of the character from right side. |
find() | return the first occurrence of character from left and it won't throw an error unlike index. |
rfind() | return the first occurrence of character from right and it won't throw an error unlike index. |
split() | Split the element to a list. |
strip() | remove whitespace from both side of the string. |
rstrip() | remove whitespace from right side of the string. |
lstrip() | remove whitespace from left side of the string. |
capitalize() | convert the first character to upper case and rest to lowercase. |
count() | count the number of times a specific character occurs in a string. |
title() | convert first character of all the words to uppercase and rest to lowercase. |
join() | Join the iterable object to a string. |
endswith() | return True if the string ends with a specified character. |
startswith() | return True if the string starts with a specified character. |
isalpha() | return True if all the character in a string is alphabets. |
isnumeric() | return True if all the character in a string is numbers. |
isalnum() | return True if all the character in a string is alphanumeric. |
isupper() | return True if all the character in a string is in uppercase. |
islower() | return True if all the character in a string is in lowercase. |