Strings in python
How to define a string in python?
There are 4 different ways of doing that, you can use single quotes
, double quotes
, triple quotes
, and triple-double quotes
!!!
Example:
s = "Traditional way of defining a string"
s1 = 'Easier way of defining the string'
s2 = '''This is another
way of defining a string, in paragraphs'''
s3 = """
"roses are red,
violets are blue,
if you like forking,
then you will like Github too."
- this entire lame poem is also a string, quite long, but a string!
"""
print(s,s1,s2,s3,sep='\n')
Output
String Indexing and Slicing
You can access the characters of the string using
+ve
or -ve
indices.
Example:
s = 'chocolate'
print(f'First character: {s[0]}') # this is f-sting**
print(f'Last character: {s[-1]}')
# s[start index: end index :steps]
# Using this you can slice a part of a string
print(f'First 3 character: {s[:3]}')
print(f'Last 3 character: {s[-3:]}')
s[:3]
is same as s[0:3]
so you needn't write the starting index.
Output
First character: c
Last character: e
First 3 character: cho
Last 3 character: ate
Now if you want to reverse the string, you can do that using slicing.
Just set steps to -1
Output
Reverse String is : etalocohc
If you want to take out the middle portion of chocolate then use this:
Output
Middle Portion : cola
And that's how you extract cola from chocolate
String concatenation
Want to concatenate multiple strings? LOL! Piece of . Just use the
+
operator. Like This:
Output
first Nameiddle nameLast name
To concatenate same string multiple times use *
:
Output
LOLLOLLOLLOLLOL
Manipulate your strings:
.replace()
Consider this example:
It's been ages with her, she has taken me for granted so I want to replace her. I don't know about real life, but this is easy in python.Output
Anjali is my girlfriend
.count()
Returns the frequency of a substring
s ="mi go, mi eat, amigo come, mi again go"
print('Considering the whole string',s.count('mi'),sep='\t')
print(f'Considering only "{s[13:25]}"',s.count('mi',13,25),sep='\t')
Output
Considering the whole string 4
Considering only ", amigo come" 1
.lower(), .upper(), .swapcase() & .capitalize()
Output
meRRy XmAs | original string
merry xmas | output of lower()
| all letters -> lowercase
MERRY XMAS | output of upper()
| all letters -> uppercase
Merry xmas | output of captalize()
| only capitalizes the initials of the first word of a string
MErrY xMaS | output of swapcase()
| self-explanatory
Merry Xmas | output of title()
| capitalizes all the initials
.islower(), .isupper(), .isdigit()
print("UPPER".isupper(),'uPPer'.isupper())
# True False
print('loWer'.islower(),'lower'.islower())
# False True
print('lol'.isdigit(),'92'.isdigit(),'3.4'.isdigit(),'/*-'.isdigit())
# False True False False
.isspace(), .istitle(), .isdecimal()
print(' '.isspace(),' s '.isspace())
# True False
print('1.2'.isdecimal(),'12'.isdecimal(),'.12'.isdecimal())
# False True False
print('Title Shitel'.istitle(),'title shitel'.istitle(),'titlE'.istitle())
# True False False
.isnumeric(), .isalpha(), .isalnum()
print('sOmethIng'.isalpha(),'anglepriya420'.isalpha())
# True False
print('12'.isnumeric(),'1.2'.isnumeric(),'numeric'.isnumeric())
# True False False
print('aasd233'.isalnum(),'123'.isalnum(),'assert'.isalnum(),'1s#'.isalnum(),'spa ce'.isalnum())
# True True True False False
.endswith(), .startswith()
Output
True False
print('Miss Hitler'.startswith('Miss'))
# True
print('Miss Hitler'.startswith('miss'))
# False
print('Miss Hitler'.lower().startswith('miss'))
# True
.split(), .join()
split()
method splits the string on the basis of a delimiter, provided by the user. returns a list of words.
By default the delemiter is set to ' '
.
Example 1
Output
This is a string
['This', 'is', 'a', 'string']
Example 2
Output
name,age,sex,roll,num
['name', 'age', 'sex', 'roll', 'num']
.join()
method concatenate all strings of a given iterable( list, tuples,.etc)
Example:
Output
name age sex roll num
REGEX
Used to search for a specific pattern in a text.
[]{}()\^$|?*+
are few characters which we need to expace using a \
as a prefix before search for these.
.
matches all characters except newline
\d
matches all the digits whereas \D
matches everthing but digits.
\w
= word | \W
= Not a word
\s
= space | \S
= not a space
\b
= word boundary | \B
= Not a word boundary
^
= Start of a string
$
= end of a string
*
= 0 or more
+
= 1 or more
?
= 0 or one match
{5}
= exact number of match
{min,max}
= range of numbers