3.Replicating String
Replication means to make a copy.It is used to repeat a string or to make a new string by repeating the multiple copies of same String.
#This is an Example of String Repetition s='Hello ' print(s*3) y='2' #Note that here y is of class string. print(y*2)
Output:-
Hello Hello Hello 22
The ‘*’ operator also known as replication operator is used for this purpose.It creates new string by repetition of the left string to number of repetitions given on right.
4.Membership Operators
Python allows for checking whether a particular character(or a sequence of characters(sub-string)) is available in the main string or not.
For it Python offers two operators:-
a.The ‘in’ operator
a. The ‘not in’ operator
a. The in operator
The in operator is a boolean-type operator.It checks whether a string is a part of main string or not.It takes two strings and return True if the first string is a sub-string of second string,otherwise it returns False.
b.The not in operator
The not in operator is also a boolean-type operator and is similar to the in operator.It is opposite to the in operator.It return True if character/substring does not exist in the given string otherwise it returns False.
To use membership operators it is required that the both operands should be of string type.
s='Hello' print('H' in s) print('Y' in s) #Python is a case sensitive language. print('HEL' in s) s1="Python" str="Python Language") print(s1 not in str) print('y' not in s)
Output:-
True False False False True
String Comparison
In python we can compare strings using ASCII value of characters. This is done using relational/comparison operators(<,>,<=,>=,==,!=).
It compares two strings character by character(starting from the first character) until it finds one that differ.After it subsequent(or the remaining characters are not taken into consideration).
>>>print("hello">"Hi")#Here as ASCII value of 'h' is greater than 'H' it will print True without comparing other elements(characters). True >>>print("Ram"=="Shyam") False >>>print("God">="Devil") True >>>print("programmer"!="developer") True >>>print("one"<"two") True#It is true only because in ASCII 't'>'o' >>>print('Python'<='C') False

You must be logged in to post a comment.