String


isalnum()

The isalnum() method returns True if all characters in the string are alphanumeric (either alphabets or numbers). If not, it returns False.

The syntax of isalnum() is:
        string.isalnum()

Return Value from isalnum()
The isalnum() returns:
  1. True if all characters in the string are alphanumeric
  2. False if at least one character is not alphanumeric

Example :- 
name = "WscubeTech123"
print(name.isalnum())
 
name = "WsCubeTech Jodhpur"
print(name.isalnum())
 
name = "133"
print(name.isalnum())

Output:-
True
False
True

isalpha()

The isalpha() method returns True if all characters in the string are alphabets. If not, it returns False.

The syntax of isalpha() is:
        string.isalpha()


Return Value from isalpha()
  1. True if all characters in the string are alphabets (can be both lowercase and uppercase).
  2. False if at least one character is not alphabet.

Example:-
name = "WsCubeTech"
print(name.isalpha())
 
# contains whitespace
name = "Python programe"
print(name.isalpha())

Output:-
True
False

islower()

The islower() method returns True if all alphabets in a string are lowercase alphabets. If the string contains at least one uppercase alphabet, it returns False.

The syntax of islower() is:
       string.islower()

Return Value from islower()
The islower() method returns:
  1. True if all alphabets that exist in the string are lowercase alphabets.
  2. False if the string contains at least one uppercase alphabet.
Example:-
s = 'this is good'
print(s.islower())
 
s = 'th!s is a1so g00d'
print(s.islower())
 
s = 'this is Not good'
print(s.islower())

Output:-
True
True
False

lower()

The string lower() method converts all uppercase characters in a string into lowercase characters and returns it.

The syntax of lower() method is:
string.lower()

Example:-
string = "THIS IS PYTHON"
print(string.lower())

Output:-

"this is python"
 

split()

The split() method breaks up a string at the specified separator and returns a list of strings.

The syntax of split() is:
str.split([separator [, maxsplit]])

split() Parameters
The split() method takes maximum of 2 parameters:
 
separator (optional)- The is a delimiter. The string splits at the specified separator.
If the separator is not specified, any whitespace (space, newline etc.) string is a separator.
maxsplit (optional) - The maxsplit defines the maximum number of splits.
The default value of maxsplit is -1, meaning, no limit on the number of splits.

Example:-
text= 'Love thy neighbor'
print(text.split())
grocery = 'Milk, Chicken, Bread'
print(grocery.split(', '))

Output:-
['Love', 'thy', 'neighbor']
['Milk', 'Chicken', 'Bread']

swapcase()

The string swapcase() method converts all uppercase characters to lowercase and all lowercase characters to uppercase characters of the given string, and returns it.
 
The format of swapcase() method is:
string.swapcase()

Example:- 

s = "WsCube Tech"
print(s.swapcase())

Output:-
"wScUBE tECH"
 

upper()

The string upper() method converts all lowercase characters in a string into uppercase characters and returns it.

The syntax of upper() method is:
string.upper()

Example:-
string = "this is python"
print(string.upper())

Output:-
"THIS IS PYTHON"

capitalize()

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

txt = "hello! welcome to WsCubeTech."
x = txt.capitalize()
print (x)
Output:- 
Hello! welcome to WsCubeTech.

count()

The string count() method returns the number of occurrences of a substring in the given string.

The syntax of count() method is:
string.count(substring, start=..., end=...)
String count() Parameters
substring - string whose count is to be found.
start (Optional) - starting index within the string where search starts.
end (Optional) - ending index within the string where search ends.

Example:- 
string = "Python is awesome, isn't it?"
substring = "is"
count = string.count(substring)
print("The count is:", count)
Output:-
The count is: 2

Example:-
count = string.count(substring, 8, 25)
print("The count is:", count)
Output:-
The count is: 1

find()

The find() method returns the index of first occurrence of the substring (if found). If not found, it returns -1.

The syntax of find() method is:
str.find(sub, start, end)

find() Parameters
sub - It's the substring to be searched in the str string.
start and end (optional) - substring is searched within str[start:end]

Return Value from find()
  • If substring exists inside the string, it returns the index of first occurence of the substring.
  • If substring doesn't exist inside the string, it returns -1.

Example :- 
quote = 'Welcome to WsCubeTech.'
 
result = quote.find('to')
print("Substring 'to':", result)
 
result = quote.find("python')
print("Substring 'python':", result)

Output:-
Substring 'let it': 8
Substring 'small': -1