learn Python By Example – String Operations

String Operations

Python 3 has three string types

  • str() is for unicode
  • bytes() is for binary data
  • bytesarray() mutable variable of bytes

 

Create some simulated text.

string = 'The quick brown fox jumped over the lazy brown bear.'

Capitalize the first letter.


string_capitalized = string.capitalize()
string_capitalized
'The quick brown fox jumped over the lazy brown bear.'

Center the string with periods on either side, for a total of 79 characters


string_centered = string.center(79, '.')
string_centered
'..............The quick brown fox jumped over the lazy brown bear..............'

Count the number of e’s between the fifth and last character


string_counted = string.count('e', 4, len(string))
string_counted
4

Locate any e’s between the fifth and last character


string_find = string.find('e', 4, len(string))
string_find
24

Are all characters are alphabet?


string_isalpha = string.isalpha()
string_isalpha
False

Are all characters digits?


string_isdigit = string.isdigit()
string_isdigit
False

Are all characters lower case?


string_islower = string.islower()
string_islower
False

Are all chracters alphanumeric?


string_isalnum = string.isalnum()
string_isalnum
False

Are all characters whitespaces?


string_isalnum = string.isspace()
string_isalnum
False

Is the string properly titlespaced?


string_istitle = string.istitle()
string_istitle
False

Are all the characters uppercase?


string_isupper = string.isupper()
string_isupper
False

Return the lengths of string

len(string)
52

Convert string to lower case


string_lower = string.lower()
string_lower
'the quick brown fox jumped over the lazy brown bear.'

Convert string to lower case


string_upper = string.upper()
string_upper
'THE QUICK BROWN FOX JUMPED OVER THE LAZY BROWN BEAR.'

Convert string to title case


string_title = string.title()
string_title
'The Quick Brown Fox Jumped Over The Lazy Brown Bear.'

Convert string the inverted case


string_swapcase = string.swapcase()
string_swapcase
'tHE QUICK BROWN FOX JUMPED OVER THE LAZY BROWN BEAR.'

Remove all leading whitespaces (i.e. to the left)


string_lstrip = string.lstrip()
string_lstrip
'The quick brown fox jumped over the lazy brown bear.'

Remove all leading and trailing whitespaces (i.e. to the left and right)

string_strip = string.strip()
string_strip
'The quick brown fox jumped over the lazy brown bear.'

Remove all trailing whitespaces (i.e. to the right)

string_rstrip = string.rstrip()
string_rstrip
'The quick brown fox jumped over the lazy brown bear.'

Replace lower case e’s with upper case E’s, to a maximum of 4

string_replace = string.replace('e', 'E', 4)
string_replace
'ThE quick brown fox jumpEd ovEr thE lazy brown bear.'

Python Example for Beginners

Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.  

Google –> SETScholars