one of the popular programming languages is python among many new and old programmers. It provides a easy to read and a pseudo-code format of program. This helps in easy understanding of the overall program structure and algorithm.
There are many ways in, which a particular python logic can be written. Below are some of the cool Python program tricks, which might help you to work with strings. Use the below tricks and get your logic working easier.
Strings
Let us see the different operations that can be performed on the below string.
word = 'Sample'
len(word)
Output
6
+---+---+---+---+---+---+
| S | a | m | p | l | e |
+---+---+---+---+---+---+
0 1 2 3 4 5 6
-6 -5 -4 -3 -2 -1
1 Concatenation
Join a string with another
string
word = 'Sample'
word + ' ' + 'trick'
Output
'Sample trick'
2 Indexed Access of Strings
A string in a python can be
indexed to perform operations on the string
Positive single
character
word = 'Sample'
word[3]
Output
p
Negative single character
word = 'Sample'
word[-2]
Output
l
String Reverse
word = 'Sample'
word[::-1]
Output
'elpmaS'
Reverse string by iterating through string contents
word = 'Sample'
for char in reversed(word):
print(char)
Output
e
l
p
m
a
S
3 Slicing of Strings
Slicing allows us to access a substring of characters from a word.
Example 1
word = 'Sample'
word[0:3]
Output
Sam
Example 2
word = 'Sample'
word[4:5]
Output
le
Example 3
word = 'Sample'
word[:5]
Output
Sampl
4 Remove leading and trailing characters
To remove space before and after a string the strip() method can be used has shown below
' Sample '.strip()
Output
Sample
'sample'.strip('ple')
Output
Sam
5 Left fill with ASCII ‘0’
To left fill with ASCII ‘0’’, we can use zfill() method to make a length of required string width.
"10".zfill(6)
Output
000010
"-10".zfill(6)
Output
-00010
6 Find Substring
The find() method can be used to extract a substring from a string
'sample'.find('am',0,5)
Output
1
'sam' in 'sample'
Output
True
7 Find if the string contains Numbers
The isalpha() method can be used to find if a string contains number
'123'.isalpha()
Output
False
'abc'.isalpha()
Output
True
'1abc'.isalpha()
Output
False
#8 Find if the string is alphanumeric
The isalnum() method can be used to find if the string contains alphanumeric and at least one character
' '.isalnum()
Output
False
'abc'.isalnum()
Output
True
9 Find if the string is having only whitespace
The isspace() method can be used to determine if the string is only whitespace character
' '.isspace()
Output
True
'Sample '.isspace()
Output
False
10 Remove Spaces on the left side of the string
The lstrip() can be used to remove whitespace characters on the left.
' sample '.lstrip()
Output
'spacious '
ss'www.example.com'.lstrip('cmowz.')
Output
'example.com'
Remove Spaces on the right side of the string
The rstrip() can be used to remove whitespace characters on the left.
' sample '.rstrip()
Output
'spacious '
'mississippi'.rstrip('ipz')
Output
'mississ'
12 Formatted String Literals
The string literals which are prefixed with ‘f’ or ’F’ are called f-string.
word = "Sample"
f"THis is a {word!r}
string."
Output
"THis is a 'Sample' string."
13 Joining multiple strings from a list
The join() method can be used to join the strings in a list
Test =
["This","is","Sample"]
print(" ".join(Test))
Output
This is Sample
14.Repeat string multiple times
Same like nos the strings can be multiplied to generate multiple string
word = "Sample"
print(word*3)
Output
SampleSampleSample
15.Splitting a sentence to word
The split() method can be used to split a sentence to multiple words to form a list
"This is a Sample".split()
Output
['This', 'is', 'a', 'Sample']
16.Find the frequent word in a sentence
The word which occurs most times in a sentence can be found using the below trick
sentence = “She was young the way an actual young person is young”.split()
most_frequent_word = max(set(sentence),key=sentence.count)
Output 'young'
Remove duplicate words in a sentence
Duplicate words in a sentence can be removed has shown below
sentence = "The sound
sounds sound".split()
list(set(sentence))
Output
['sound', 'The', 'sounds']
Remove duplicate words retain the sentence order
The below trick removes the duplicate word keeping the order of words in the sentence.
from collections import OrderedDict
sentence = "The sound sounds
sound".split()
list(OrderedDict.fromkeys(sentence).keys())
Output
['The', 'sound', 'sounds']
Reversing a sentence
The reverse() method can be used to reverse a sentence
sentence = "This is
Sample".split()
sentence.reverse()
print(sentence)
Output
['Sample', 'is', 'This']
Combine list using zip() method
The zip() method can be used to combine two seperate list of strings.
Name = ['Tom', 'Marry', 'Jon']
Age = ['35', '30', '40']
for Name, Age in zip(Name,Age):
print(Name,Age)
Output
Tom 35
Marry 30
Jon 40
Top comments (0)