Strings in Python (Part-2)
So, let’s continue our Python programming journey from where we left that is ‘Strings’. So, we will see more functioning of strings in Python programming. Let’s start with slicing strings.
Slicing Strings
- We can also look at any continuous section of a string using a colon operator.
- The second number is one beyond the end of the slice — “Up to but not including”.
- If the second number is beyond the end of the string, it stops at the end.
- If we leave off the first number or the last number of the slice, it is assumed to be the beginning or end of the string, respectively. Following are some examples on slicing of strings:
s = 'HELLO PYTHON'
print(s[0:4]) # Output: HELL
print(s[6:7]) # Output: P
print(s[6:20]) # Output: PYTHON
print(s[:2]) # Output: HE
print(s[8:]) # Output: THON
print(s[:]) # Output: HELLO PYTHONString Concatenation
When the ‘+’ operator is applied to strings, it means ‘concatenation’. For example:
a = 'Hello'
b = a + 'There'
print(b) # Output: HelloThere
c = a + ' ' + b
print(c) # Output: Hello ThereUsing ‘in’ as a logical operator
- The 'in' keyword can also be used to check whether a string is in another string or not.
- The ‘in’ expression is a logical expression that returns ‘True’ or ‘False’ & can be used in an if statement. For example:
fruit = 'Banana'
'n' in fruit
'm' in fruit
'nan' in fruit
if 'a' in fruit:
print('Found it!')Output of above code will be:
True
False
True
Found it!String Library
- Python has a number of string functions which are in the string library.
- These functions are already built into every string—we invoke them by appending the function to the string variable.
- These functions do not modify the original string, instead they return a new string that has been altered. For example:
greet = 'Hello Bob'
down = greet.lower()
print(down) # Output: hello bob
print(greet) # Output: Hello Bob
print('Hi There'.lower()) # Output: hi thereSearching a String
- We use ‘find()’ function to search for a substring within another string.
- The ‘find()’ function finds the first occurrence of the substring.
- If the substring is not found, ‘find()’ returns: -1. For example:
fruit = 'banana'
pos = fruit.find('na')
print(pos) # Output: 2
aa = fruit.find('z')
print(aa) #Output: -1Remember that string position starts at zero.
Making everything ‘Upper Case’
- You can make a copy of a string in lower case or upper case.
- Often when we are searching for a string using find() we first convert the string to lower case, so we can search a string regardless of case. For example:
greet = 'Hello Bob'
big = greet.upper()
print(big) # Output: HELLO BOBSearch & Replace
- The ‘replace()’ function is like a “search and replace” operation in a word processor.
- It replaces all occurrences of the search string with the replacement string. For example:
greet = 'Hello Bob'
nstr = gret.replace('Bob', 'Jane')
print(nstr) # Output: Hello Jane
nstr = greet.replace('o', 'x')
print(nstr) # Output: Hellx BxbStripping Whitespace
- Sometimes we want to take a string & remove whitespace at the beginning and/or end.
- The ‘lstrip()’ & ‘rstrip()’ functions remove whitespace at the left or right.
- The ‘strip()’ function removes both beginning & ending whitespace. For example:
greet = ' Hello Bob '
print(greet.lstrip()) # Output: 'Hello Bob '
print(greet.rstrip()) # Output: ' Hello Bob'
print(greet.strip()) # Output: 'Hello Bob'Prefixes
It is a type of boolean expression which gives results about starting letter.
line = 'Please have a nice day'
line.startswith('Please') # Output: True
line.startswith('p') # Output: FalseParsing & Extracting
Analyzing strings into a logical syntactic component is known ‘Parsing’ and extracting a part of string is known as ‘Extraction’. For example:
data = 'Hello Issac.newton@abc.ef.gh Thu Feb 21:14:35 2022'
atpos1 = data.find('@')
print(atpos1) # This command will print the position of '@'.
atpos2 = data.find(' ', atpos1)
print(atpos2) # This command will print the postion of next space which arrives after '@'.
text = data[atpos1+1:atpos2)
print(text) # This command will print the text which exists between two positions which are 'atpos1' and 'atpos2'Output of above code will be:
18
27
abc.ef.ghThis will be the end of ‘Strings in Python’. Please do clap and follow me so that you get all the updates when I post my next article on Medium. See you soon and Good Luck to all of you!!
Get Pranshu Jain’s stories in your inbox
Join Medium for free to get updates from this writer.
My profiles : LinkedIn | GitHub
More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord.
Interested in scaling your software startup? Check out Circuit.








