Some examples of [] metacharacter

Lesson Details:
June 29, 2020


I: Introduction

Python is one of the most popular programming languages used for web applications, data analysis, scientific computing and many other purposes. It is an open source language with a very high level of abstraction, meaning that it has features like higher-order functions, dynamic typing and a large standard library. Python programs tend to be much shorter than equivalent ones written in other languages, such as C++ or Java, but they are also easier to write and maintain.

II: Body

To learn python programming check out this video series by John Purcell: https://www.youtube.com/playlist?list=PLrvxB3MVCeijm0u3LnFd4x1CKqLuw2xDt

This is a list of some [] metacharacter that can be used in python strings.

fill() – Fills a string with a character or a sequence of characters.

join() – Joins a sequence of strings into a single string.

ljust() – Left justify a string.

lower() – Converts string to lowercase.

replace() – Replaces characters in a string with another character or a sequence of characters.

rfind() – Finds the last occurrence of a substring within a string.

rindex() – Returns the index where a substring is first found.

rjust() – Right justify a string.

rstrip() – Removes whitespace from the right side of the string.

split() – Splits a string into a list of words.

There are also two built-in methods – split and rsplit – which use regular expressions to split a string at any delimiter character, and return a list of the substrings separated by the delimiter character. These methods take optional arguments that can be used to provide instructions to the method about what kind of regular expression will be used to separate the substrings, and whether the returned values should include empty strings that were part of the original string, but not part of any substrings returned by the regular expression. The regular expression and empty string parameters can be set to None if they don’t apply:

>>> line = 'The quick brown fox jumps over the lazy dog.' >>> line.split(' ') ['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog.'] >>> line.rsplit(' ', 1) ['The', 'quick brown fox', 'jumps', 'over', 'the lazy dog.'] >>> line.rsplit(None, 1) ['The', 'quick brown fox', ''] >>> line.rsplit(' ', 2) ['The', 'quick brown fox jumps over the lazy dog.'] >>> line.rsplit(None, 2) ['The', 'quick brown fox'] >>> # Use regular expressions to count word frequencies >>> text = 'Hello world! How are you? This is my new website.' >>> text.split(' ', 3) [u'Hello', u' world!', u' How are you?', u' This is my new website.'] >>> [word[0] for word in text.split(' ', 3)] [u'H', u'e', u'l', u'l', u'o', u' ', u'w', u'o', u'r', u'l', u'd', u'!', u' ', u'h', u'o', u'w', u' ', u'a', u't', u're', u're'] >>> [word[0] for word in text.rsplit(' ', 3)] [u'H', u'e', u'l', u'l', u'o'] >>> [word[0] for word in text.rsplit(None, 3)] [u'H', u'e', u'l', u'l', u'o'] >>> [word[0] for word in text.split(None, 3) if len(word) > 4] [u'How are you? This is my new website.'] >>> [word[0] for word in text.rsplit(None, 3) if len(word) > 4] [u'How are you? This is my new website.'] >>> [word[0] for word in text.split((None,), 3)] [u'Hello world! How are you? This is my new website.'] >>> [word[0] for word in text.rsplit((None,), 3)] [u'Hello world! How are you?'] >>> # Using regular expressions with rsplit and split is more powerful but requires ... ... # more careful attention to the details of regular expressions: >>> line = 'He said she had a small mouth.' >>> line.split(' ', 1)[1].split('!')[0] 'She had a small mouth.' >>> line.rsplit(' ', 1)[1].split('!')[0] 'She had a small mouth!' >>> line = 'He said she had a small mouth.' >>> line . split ( '(.)!?' , 1 ) [ '' , 'she had a small mouth.' ] >>> line . rsplit ( '(.)!?' , 1 ) [ '' , 'she had a small mouth!' ]

loader
Course content