Python has many built in methods for string. Let's take a closer look at them.
They write down what each method in the Python program should do while developing the Python program.
Ex:
print()
capitalize()
The job of the capitalize() method is to convert the first letter of a sentence to uppercase.
title="vijanthi tutorial"
print(title.capitalize())
''''
output:
Vijanthi tutorial
''''
Description:-
Change the first letter 'v' to uppercase in the sentence Vijanthi tutorial.
count()
The job of count() is to count how many times a particular value occurs in a sentence.
Syntax
count(value,start,end)
value -> This is a string. This is what you will look for in sentence.
start -> This is an integer that indicates where to start the search. Its default value is 0.
end -> This is an integer indicating where to search. Its default value is the end of the string.
a="i love java"
print(a.count("java"))
''''
output:
1
''''
Description:-
The word java appears only once.
a="i love java"
print(a.count("v"))
''''
output:
2
''''
Description:-
In the sentence "I love Java" the word "v" appears twice.
a="javascript"
print(a.count("a",0,3))
''''
output:
1
''''
Description:-
count(“a”,0,3) This method will count the number of occurrences of the character a in the word javascript.
It also counts the number of times the character 'a' is present in the javascript word starting at the starting index position 0 and ending at the ending index position 3.
islower()
The function of islower() method is to return true if all characters in a string are lower case and false otherwise.
a="java"
b="Java"
print(a.islower())
print(b.islower())
''''
output:
True
False
''''
Description:-
All the characters in the string java in the variable a are lower case. So the islower() method returns the value true.
Since the first letter of the Java string in the variable b is uppercase, the islower( ) method returns the value false.
lower()
The lower() method converts a capital letter to a lowercase letter.
The word uppercase refers to a capital letter. The word lowercase refers to a small letter.
a="Java"
b="JAVA"
print(a.lower())
print(b.lower())
''''
output:
java
java
''''
isupper()
The isupper() method returns true if all characters inside a string are capital letters and false otherwise.
a="JAVA"
b="Java"
print(a.isupper())
print(b.isupper())
''''
output:
True
False
''''
swapcase()
The function of the swapcase() method is to convert an uppercase character to lowercase and a lowercase character to uppercase and so on.
name="Vijanthi Tutorial"
print(name.swapcase())
''''
output:
VIJANTHI TUTORIAL
''''
title()
When using the title() method, the first letter of each word in the string is converted to uppercase.
a="welcome to java"
print(a.title())
''''
output:
Welcome To Java
''''
istitle()
The function of the istitle() method is to return true if the first letter of each word in a string is an uppercase letter and false otherwise.
a="welcome to java"
b="Java Node"
print(a.istitle())
print(b.istitle())
''''
output:
False
True
''''
startswith()
Returns true if the value passed inside the startswith() method is the starting value of the string and false otherwise.
msg="hey how are you?"
print(msg.startswith("hey"))
print(msg.startswith("how"))
''''
output:
True
False
''''
Description:-
startswith(“hey”) Returns true if the value hey is the starting value of the string.
startswith(“how”) hey does not have a starting value of string, so returns false.
endswith()
Returns true if the value passed inside the endswith() method is the last value of the string and false otherwise.
msg="i love code"
print(msg.endswith("i"))
print(msg.endswith("code"))
''''
output:
False
True
''''
Description:-
endswith(“i”) returns FASLE because the string is not at the end.
endswith(“code”) Returns True because the value ends with the end of the string.
isnumeric()
The function of the isnumeric() method is to return true if everything in the string is a number and false otherwise.
a="123"
b="123abc"
print(a.isnumeric())
print(b.isnumeric())
''''
output:
True
False
''''
isalpha()
The function of the isalpha() method is to return true if the string contains all characters and false otherwise.
a="hello"
b="hello123"
print(a.isalpha())
print(b.isalpha())
''''
output:
True
False
''''
isalnum()
When using the isalnum() method, it returns true if all the characters in the string are alphanumeric and false otherwise.
Alphanumeric refers to alphabets (a-z) and numbers (0-9).
Blankspace,!,%,#,…etc are not Alphanumeric.
a="123"
b="good"
c="java123"
d="java 123"
print(a.isalnum())
print(b.isalnum())
print(c.isalnum())
print(d.isalnum())
''''
output:
True
True
True
False
''''
Description:-
“123” returns true because everything in this string is numeric.
"god" returns true since all of them are alphabets.
“Java123” returns true because it contains alphabet and numeric.
"Java 123" returns false because a space is left between two words. Because a blank space is not an alphanumeric value.
find()
find() searches for a specified value from the string and returns its current position. If the value we are looking for is not in the given string, it will return "-1".
a="welcome to vijanthi tutorial"
print(a.find("to"))
''''
output:
8
''''
Description:-
find(“to”)-> where the value “to” is in the given string. Its index position is 8. So it returns that value to us.
index()
The index() method works similarly to the find() method. Both index() and find() do the same thing.
a="welcome to vijanthi tutorial"
print(a.index("to"))
''''
output:
8
''''
replace()
A particular value in a string can be replaced using the replace() method.
a="i love dogs"
print(a.replace("love","like"))
''''
output:
I like dogs
''''
Explain:-
replace (“love”, “like”) i.e. wherever the word “love” occurs, it removes the word love and gives the word like.
a="i love java,i love css,i love html"
print(a.replace("i","we",2))
''''
output:
we love java, we love css, i love html
''''
Description:-
replace(“i”,,”we”,,”2″,) How this works is to remove i wherever it occurs and replace it with the word we. What the value of 2 does is replace only the first 2 “i”s in the string. does not replace the "i" that follows it.
strip()
The strip() method is used to remove a specific value from a string.
a="#$java"
print(a.strip("#$"))
''''
output:
java
''''
Description:-
In this the value '#$' is present in the string we have given, so it removes those words from the string. After that, it displays the existing string "java" on the output screen.