Thursday, 26 December 2019

Python String -Part 2


Python istitle() method returns True if the string is a titlecased string. Otherwise returns False.
Syntax
istitle() 
Parameters
No parameter is required.
Return
It returns either True or False.

Python String istitle() Method Example
# Python istitle() method example 
# Variable declaration 
str = “Welcome To Javatpoint” # True 
# Calling function 
str2 = str.istitle() 
# Displaying result 
print(str2) 
str = “hello javatpoint”    # False 
str2 = str.istitle() 
print(str2) 
Output:
True
False

Python isupper() method returns True if all characters in the string are in uppercase. It returns False if characters are not in uppercase.
Syntax
isupper() 
Parameters
No parameter is required.
Return
It returns either True or False.

Python String isupper() Method Example
# Python isupper() method example 
str = “WELCOME TO JAVATPOINT” 
str2 = str.isupper() 
print(str2) 
str3 = “Learn Java Here.” 
str4 = str3.isupper() 
print(str4) 
Output:
True
False

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 throws an exception TypeError if iterable contains any non-string value.
It allows various iterables like: List, Tuple, String etc.
Syntax
join(iterable) 
Parameters
iterable : iterable object like: List, Tuple, String etc.
Return
It returns a new string or an exception TypeError if iterable contains any non-string value.

Python String join() Method Example
A simple example which implements join() method with the List iterable, see the example below.
# Python join() method example 
# Variable declaration 
str = “:”   # string 
list = ['1','2','3']    # iterable 
# Calling function 
str2 = str.join(list) 
# Displaying result 
print(str2) 
Output:
1:2:3

Python String join() Method Example
A list iterable join with empty string and produce a new string, see the example below
# Python join() method example 
# Variable declaration 
str = ““   # string 
list = ['J','a','v','a','t','p','o','i','n','t']    # iterable 
# Calling function 
str2 = str.join(list) 
# Displaying result 
print(str2) 
Output:
Javatpoint

Python ljust() method left justify the string and fill the remaining spaces with fillchars. This method returns a new string justified left and filled with fillchars.
Syntax
ljust(width[, fillchar]) 
Parameters
width : width of the given string.
fillchar : characters to fill the remaining space in the string. It is optional.
Return
It returns a left justified string.
                                                           
Python String ljust() Method Example
# Python ljust() method example 
# Variable declaration 
str = 'Javatpoint' 
# Calling function 
str = str.ljust(15,”$”) 
# Displaying result 
print(str) 
Output:
Javatpoint$$$$$

Python lower() method returns a copy of the string after converting all the characters into lowercase.
Syntax
lower() 
Parameters
No parameter.
Return
It returns a lowercase string.

Python String lower() Method Example
See a simple example in which string is converting into lowercase.
# Python lower() method example 
# Variable declaration 
str = “Javatpoint” 
# Calling function 
str = str.lower() 
# Displaying result 
print(str) 
Output:
javatpoint

Python lstrip() method is used to remove all leading characters from the string. It takes a char type parameter which is optional. If parameter is not provided, it removes all the leading spaces from the string.
Syntax
lstrip([chars]) 
Parameters
chars (optional) : A list of chars
Return
It returns a string.

Python String lstrip() Method Example
Here, we are adding some special chars to the string and applying lstrip to remove leading chars.
# Python lstrip() method example 
# Variable declaration 
str =  “$$$$-Javatpoint-$$$$” 
# Calling function 
str2 = str.lstrip('$') 
# Displaying result 
print(str) 
print(str2) 
Output:
$$$$-Javatpoint-$$$$
-Javatpoint-$$$$

Python len() function is an inbuilt function in Python programming language that returns the length of the string.
Syntax:
len(string)
Parameters:
It takes string as the parameter.
Return Value:
It returns an integer which is the length of the string.

# Python program to demonstrate the use of
# len() method  
# Length of below string is 5
string = “geeks”
print(len(string))
# Length of below string is 15
string = “geeks for geeks”
print(len(string))
Output:
5
15

The string maketrans() method returns a mapping table for translation usable for translate() method.
It creates a Unicode representation of each character for translation.
This translation mapping is then used for replacing a character to its mapped character when used in translate() method.
The syntax of maketrans() method is:
string.maketrans(x[, y[, z]])
Here, y and z are optional arguments.
String maketrans() Parameters
The maketrans() method takes 3 parameters:
x - If only one argument is supplied, it must be a dictionary.
The dictionary should contain 1-to-1 mapping from a single character string to its translation OR a unicode number (97 for 'a') to its translation.
y - If two arguments are passed, it must be two strings with equal length.
Each character in the first string is a replacement to its corresponding index in the second string.
z - If three arguments are passed, each character in the third argument is mapped to None.
Return value from String maketrans()
The maketrans() method returns a translation table with a 1-to-1 mapping of a Unicode ordinal to its translation/replacement.
Example:
# example dictionary
dict = {“a”: “123”, “b”: “456”, “c”: “789”}
string = “abc”
print(string.maketrans(dict))
Output
{97: '123', 98: '456', 99: '789'}

Python partition() method splits the string from the string specified in parameter. It splits the string from at the first occurrence of parameter and returns a tuple. The tuple contains the three parts before the separator, the separator itself, and the part after the separator.
It returns an empty tuple having seperator only, if the seperator not found.
The method Syntax is given below.
Syntax
partition(sep) 
Parameters
sep: A string parameter which separates the string.
Return
It returns a tuple, A 3-Tuple.

Python String partition() Method Example
First, let's see simple use of partition method.
# Python partition() method example 
# Variable declaration 
str = “Java is a programming language” 
# Calling function 
str2 = str.partition(“is”) 
# Displaying result 
print(str2) 
# when seperate from the start 
str2 = str.partition(“Java”) 
print(str2) 
# when seperate is the end 
str2 = str.partition(“language”) 
print(str2) 
# when seperater is a substring 
str2 = str.partition(“av”) 
print(str2) 
Output:
('Java ', 'is', ' a programming language')
('', 'Java', ' is a programming language')
('Java is a programming ', 'language', '')
('J', 'av', 'a is a programming language')
Python String partition() Method Example
if the separator is not found, It returns a tuple containing string itself and two empty strings. See the example below.
# Python partition() method example 
# Variable declaration 
str = “Java is a programming language” 
# Calling function 
str2 = str.partition(“not”) 
# Displaying result 
print(str2) 
Output:
('Java is a programming language', '', '')

Python replace() Method return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.
Syntax
replace(old, new[, count]) 
Parameters
old : An old string which will be replaced.
new : New string which will replace the old string.
count : The number of times to process the replace.
Return
It returns string

Let's see some examples of replace() method to understand it's functionality.
Python String replace() Method Example
# Python replace() method example 
# Variable declaration 
str = “Java is a programming language” 
# Calling function 
str2 = str.replace(“Java”,”C”) 
# Displaying result 
print(“Old String: \n”,str) 
print(“New String: \n”,str2) 
Output:
Old String:
Java is a programming language
New String:
C is a programming language

Python rfind() method finds a substring in in the string and returns the highest index. It means it returns the index of most righmost matched subtring of the string. It returns -1 if substring not found.
Syntax
rfind(sub[, start[, end]]) 
Parameters
sub : substring to be searched.
start (optional) : starting index to start searching.
end (optional) : end index where to search stopped.
Return
It returns either index of substring or -1.

Python String rfind() Method Example
Let's have a simple example to implement the rfind() method. It returns highest index of the substring.
# Python rfind() method example 
# Variable declaration 
str = “Learn Java from Javatpoint” 
# calling function 
str2 = str.rfind(“Java”) 
# displaying result 
print(str2) 
Output:
16

Python String rfind() Method Example
One more example to understand the working of rfind() method.
# Python rfind() method example 
# Variable declaration 
str = “It is technical tutorial” 
# calling function 
str2 = str.rfind(“t”) 
# displaying result 
print(str2) 
Output:
18

No comments:

Post a Comment

Python Datatype Interview Questions

Q)  What is Python? Python is a high-level, interpreted, interactive, and object-oriented scripting language. Python is designed to be...