Strings in Python
Strings in Python
Strings in Python
# String operators
concatenate (+): we can concatenate two or more strings using plus (+)
operator
'Python' + 'Language' # 'PythonLanguage'
'55' + '65' # '5565'
replication (*): It performs replication instead of multiplication. The * operator
when used with numbers, it does multiplication and if we used with string and
integer, the i* operator replicate the string with given number of times.
'python'*3 # 'pythonpythonpython'
3 * '@Hello' #'@Hello@Hello@Hello'
Comparison Operator:
All the relational operator works with strings for comparing strings together.
'python' == 'python' will give True
'python' == 'Python' will give False
Example:
str1 = input('Enter the first string: ')
str2 = input('Enter the second the string: ')
if str1 == str2:
print('Both strings are equal')
else:
print('Both strings are not equal')
Note: But to compare string for less than or greater than, comparison done on the
basis of ASCII or Unicode values.
Characters Ordinal Values
‘0’ to ‘9’ 48 to 57
‘A’ to ‘Z’ 65 to 90
sample[7] ''
sample[-5] 'y'
mystr = '123'
mystr.isalnum() will give True
mystr = ''
mystr.isalnum() will give False
------------------------------------------------------------------------------
# isalpha(): It returns True if all the characters are alphabetic
mystr = 'python'
mystr.isalpha() will give True
mystr = 'python12'
mystr.isalpha() will give False
mystr = '123'
mystr.isalpha() will give False
--------------------------------------------------------------------------------
# isdigit(): It returns True if all the characters are digits.
mystr = '123'
mystr.isdigit() will give True
mystr = 'python123'
mystr.isdigit() will give False
----------------------------------------------------------------------------------
# islower(): It returns True if all the characters are in lower case.
mystr = 'python'
mystr.islower() will give True
mystr = 'Python'
mystr.islower() will give False
mystr = 'PYTHON'
mystr.islower() will give False
mystr = ''
mystr.islower() will give False
--------------------------------------------------------------------------------------------
# isspace(): It returns True if there are only whitespaces.
mystr = ' '
mystr.isspace() will give True
mystr = 'Python'
mystr.isupper() will give False
mystr = 'P123'
mystr.isupper() will give True
mystr = 'P___'
mystr.isupper() will give True
-----------------------------------------------------------------------------------------
# lower(): converts string in lowercase.
mystr ='PYTHON'
mystr.lower() will give 'python'
------------------------------------------------------------------------------------------
# upper(): converts string in uppercase.
mystr ='python'
mystr.upper() will give 'PYTHON'
-------------------------------------------------------------------------------------------
# lstrip(): It removes the leading characters and returns the string
# if whitespaces are there, then it will be removed too
mystr = ' Python'
mystr.lstrip() will give 'Python'
mystr = 'Python'
mystr.lstrip('P') will give 'ython'
mystr = '@@@Python@@'
mystr.lstrip('@') will give 'Python@@'
mystr = '@@@Python@@'
mystr.lstrip('th') will give '@@@Python@@'
mystr = 'Pythob'
mystr.lstrip('tyP') will give 'hob'
---------------------------------------------------------------------------------------------------------
mystr = 'Python'
mystr.rstrip('on') will give 'Pyth'
mystr = '$Python$$'
mystr.rstrip('$')
---------------------------------------------------------------------------------------------
# strip(): It returns the leading and trailing whitespaces around string or matching
substring.
mystr = '@@@Python@@'
print(mystr.strip('@')) will give Python
----------------------------------------------------------------------------------------------
# count(): It returns the number of occurrences of the substring in a string.
mystr = 'Python is best for AI and Ml. Python is best for Games too'
mystr.count('best') #2
------------------------------------------------------------------------------------------------
# index():It returns the lowest index, where substring is found.
mystr = 'Python is the most amazing language'
print(mystr.index('most')) # 14
--------------------------------------------------------------------------------------------------
# join(): It joins a string or character after each character of the string iterator.
'-'.join('Hello')
'H-e-l-l-o'
------------------------------------------------------------------------------------------------------
# split(): It splits the string on the basis of given string or character and returns as
a list of string.
mystr = 'python was developed by Guido Van Rossum'
mystr.split() # by default it splits with space.
['python', 'was', 'developed', 'by', 'Guido', 'Van', 'Rossum']
-------------------------------------------------------------------------------------------------------