Interesting Cool Python Tricks for working with Strings

“Python tricks” is a tough one, cuz the language is so clean.

Ravi
Ravi
Aug 12, 2020 · 5 min read

Python programming language is one of the popular programming languages 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 '
'www.example.com'.lstrip('cmowz.')
Output
'example.com'

#11 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 Search for multiple prefixes in a string

The startswith() and endswith() method can be passed with multiple patterns of substrings to check if it is present.

"sample".startswith(("sam","Sam"))
Output
True
"sample".endswith(("ple","ple."))
Output
True

#16 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']

#17 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'

#18 Find how many occurences of words in a sentence

The below trick helps in finding, how many times the word is present in the sentence sorted by the increasing rate of word occurence.

from collections import Counter
sentence = “She was young the way an actual young person is young”.split()
Counter(sentence)
Output
Counter({'young': 3, 'She': 1, 'was': 1, 'the': 1, 'way': 1, 'an': 1, 'actual': 1, 'person': 1, 'is': 1})

#19 Convert all the words to capital in a sentence

The map() method can be used to make all words to capital letters.

sentence = "Quick brown fox".split()
list(map(str.capitalize,sentence))
Output
['Quick', 'Brown', 'Fox']

#20 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']

#21 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']

#22 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']

#23 Splitting words to multiple string

The below trick can be used to split a sentence into multiple words and storing in a string variable.

sentence = "This is Sample".split()
first_word, second_word, third_word = sentence
print(first_word)
print(second_word)
print(third_word)

#24 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

#25 Sort words in a list using sorted() method

The sorted() method can be used to sort the list of strings.

sorted(['string1','string2','string3'],reverse=True)Output
['string3', 'string2', 'string1']

References

  1. Python Documents
  2. PyTricks

“Python tricks” is a tough one, cuz the language is so clean. E.g., C makes an art of confusing pointers with arrays and strings, which leads to lotsa neat pointer tricks; APL mistakes everything for an array, leading to neat one-liners; and Perl confuses everything period, making each line a joyous adventure <wink>.
— Tim Peters

Happy Coding!!!

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data…

Sign up for Analytics Vidhya News Bytes

By Analytics Vidhya

Latest news from Analytics Vidhya on our Hackathons and some of our best articles! Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Ravi

Written by

Ravi

Explore to understand. “One should use information and logic as a drunkard would use a lamp post – only for support, not for illumination.-Sadhguru”

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Ravi

Written by

Ravi

Explore to understand. “One should use information and logic as a drunkard would use a lamp post – only for support, not for illumination.-Sadhguru”

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store