Python Strings
Python Strings
Syntax:
In python, strings are treated as the sequence of characters which means that python
doesn't support the character data type instead a single character written as 'p' is
treated as the string of length 1.
str[0]=H
str[1]=E
str[4]=O
Example:
str1 = "Hello"
str2 = " World"
print(str1*3) # prints HelloHelloHello
print(str1+str2) # prints Hello world
print(str1[4]) # prints o
print(str1[2:4]) # prints ll
print('w' in str1) # prints false as w is not present in str1
print('Wo' not in str2) # prints false as Wo is present in str2.
print(r'Hello\n world') # prints Hello\n world as it is written
print("The string str1 : %s"%(str1)) # prints The string str : Hello
Output:
HelloHelloHello
Hello World
o
ll
False
False
Hello\n world
The string str1 : Hello
Python String functions
Python provides various in-built functions that are used for string handling. Those are
len()
lower()
upper()
replace()
join()
split()
find()
index()
isalnum()
isdigit()
isnumeric()
islower()
isupper()
☞ len()
In python string len() function returns length of the given string.
Syntax:
len(string)
Output:
15
☞ lower ()
In python, lower() method returns all characters of given string in lowercase.
Syntax:
str.lower()
Output:
python
☞ upper ()
In python upper() method converts all the character to uppercase and returns a
uppercase string.
Syntax:
str.upper()
Output:
PYTHON
☞ replace()
In python replace() method replaces the old sequence of characters with the new
sequence. If the optional argument count is given, only the first count occurrences are
replaced.
Syntax:
# Displaying result
print("\n Old String: \n",str)
print("New String: \n",str3)
Output:
python
PYTHON
Old String:
Java is Object-Oriented and Java is Portable
New String:
Python is Object-Oriented and Python is Portable
Old String:
Java is Object-Oriented and Java is Portable
New String:
Python is Object-Oriented and Java is Portable
New String:
Python is Object-Oriented and Portable
☞ join()
Python join() method is used to concat a string with iterable object. It returns a new
string which is the concatenation of the strings in iterable. It allows various iterables like:
List, Tuple, String etc.
Syntax:
str.join(sequence)
Output:
N:N:R:G
☞ split()
In python split() method splits the string into a comma separated list. It separates string
based on the separator delimiter. The string splits according to the space if the delimiter
is not provided.
Syntax:
str.split([sep=”delimiter”])
sep: It specifies the delimiter as separator.
Output:
Java is a programming language
['Java', 'is', 'a', 'programming', 'language']
Java, is, a, programming, language
['Java', 'is', 'a', 'programming', 'language']
☞ find()
In python find() method finds substring in the whole string and returns index of the first
match. It returns -1 if substring does not match.
Syntax:
str.find(sub[, start[,end]])
sub :it specifies sub string.
start :It specifies start index of range.
end : It specifies end index of range.
Output:
7 -1 12 7
☞ index()
In python index() method is same as the find() method except it returns error on failure.
This method returns index of first occurred substring and an error if there is no match
found.
Syntax:
index(sub[, start[,end]])
sub :it specifies sub string.
start :It specifies start index of range.
end : It specifies end index of range.
Output:
7
12
7
Substring not found
☞ isalnum()
In python isalnum() method checks whether the all characters of the string is
alphanumeric or not. A character which is either a letter or a number is known as
alphanumeric. It does not allow special chars even spaces.
Syntax:
str.isalnum()
Output:
True
True
True
False
False
☞ isdigit()
In python isdigit() method returns True if all the characters in the string are digits. It
returns False if no character is digit in the string.
Syntax:
str.isdigit()
Output:
True
False
False
False
True
False
☞ isnumeric()
In python isnumeric() method checks whether all the characters of the string are
numeric characters or not. It returns True if all the characters are numeric, otherwise
returns False.
Syntax:
str.isnumeric()
Output:
True
False
False
False
True
True
☞ islower()
In python string islower() method returns True if all characters in the string are in
lowercase. It returns False if not in lowercase.
Syntax:
str.islower()
Output:
True
False
True
☞ isupper()
In python string isupper() method returns True if all characters in the string are in
uppercase. It returns False if not in uppercase.
Syntax:
str.isupper()
Output:
True
False
True