The Startup
Published in

The Startup

Most Common String Methods in Python

I would like to share with you some of the basic Python methods usage. While doing this I will use the lyrics of the song that I really loved when I was a teenager; “Bye Bye Bye” from N’SYNC..:)

Here is the part of the lyrics of the song…

Common String Methods To Use

  • count( )
  • len( )
  • strip( )
  • upper( )
  • lower( )
  • title( )
  • capitalize( )
  • replace( )
  • split( )

My main aim is to calculate the percentage distribution of characters in string. But first let’s do some tricks with this lyrics by using Python methods;)

Using the count( ) method returned the number of times a specified value appears in the string. I would like to point out that Python is a case-sensitive programming language, so “B” is not same with “b”.

Using the len( ) method returned the number of whole characters in the string (including spaces and punctuation marks).

When I’d like to access to a specific character in string, I could use indexing and slicing. The index begins from “0” continues by incrementing 1 including the spaces and punctuation marks until the end of the string. I must also say that when I use indexing like “lyrics[0:33]”, it begins to be returned from 0 index value until the 33 (but 33rd index value is not being returned).

I removed the character that I setted into the strip ( ) method at the beginning and the end of the string (space is the default character to remove).

When I’d like to be returned all characters of the string to upper case, I use upper ( ) method.

If I’d like to be returned all characters of the string to lower case, I use lower ( ) method.

Either if I’d like to be returned the first character of each word in the string to upper case, I use title ( ) method.

And lastly, when I’d like to be returned the first character of the string to upper case, I use capitalize ( ) method.

Let’s get back again to my main aim. Calculating the percentage distribution of characters and words in string.

Firstly I used lower ( ) method for not to be affected by case-sensitive. Then I used replace ( ) method that I thought is very useful. I got rid of the characters that I thought is not useful like “,”. I think that the replace( ) method could also be used instead of the strip method.

By using split ( ) method, I splitted the string into a list. Thus I realized that string had 73 words.

I also realized that the word I search (“bye”) appeared 10 times in splitted_lyrics.

I could now calculate the percentage distribution of the word I am looking for as above. The result turns out to be 13.69%. It is not a low rate:)

Now, let’s calculate the percentage distribution of all characters in string.

Firstly, I got rid of the characters that I didn’t want to use with replace ( ) method. Then the number of whole characters in the string were returned 253 with len ( ) method.

I created a list from alphabet. Then using “for loop” and “if condition”, I added the number of each letters in the string to the empty dictionary. Thus I got how many of each letters there are in the string.

I defined a function with “def” keyword, it called “perc_character( )”. I wanted to see the percentage of letters that I searched for. Defining a function for iterating operations is a very practical approach.

By using “for loop” and “perc_character()” function, I calculated the percentage distribution of all characters in the string.

Finally, I sorted the characters of the string by distribution of percentage. The most used character was “e” with 13.83%, while the least used character was “p” with 0.4%.

I hope you enjoyed!:) See you next time..

References

https://www.geeksforgeeks.org/python-sort-python-dictionaries-by-key-or-value/

https://www.w3schools.com/python/python_datatypes.asp

https://www.programiz.com/python-programming/string

--

--

--

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +756K followers.

Recommended from Medium

Service to Service Authentication on Kubernetes

A diagram of a client app authenticating with a bearer token to a server app with OAuth2-Proxy in front of it.

Elegant Way To Verify That Methods Throws Exception In .NET

Homing Missile Part 2

How to migrate large Git repositories

How Do Recent Microsoft 365 Announcements affect Microsoft 365 Business?

How to Handle Bounced Emails using AWS

Introduction to JAM Stack

Python is deprecating some old modules in their 3.13 version of the language

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
Ozan Güner

Ozan Güner

Data Analyst | Data Science Enthusiast

More from Medium

Understanding Regex with Python

Why are Python and Machine Learning Soulmates?

Learning to implement Linear Regression from scratch in python.

Linear regression

Basics of python for machine learning.