01 logo

Python-Easy Working with strings!

Text Analytics using Python

By Priyanka DandalePublished 3 years ago 5 min read
1

Overview

Beginners guide for strings in Python. We will cover the Zen of Python and string literals and operations.

1. Introduction - The Zen of Python

A Pythoneer once wrote 20 aphorisms stating how one can write beautiful and clean code with Python. Came to be known as “The Zen of Python”, these aphorisms exploded amongst Python developers. Software engineer Tim Peters wrote these BDFL’s (Benevolent Dictator For Life, a nickname of Guido van Rossum, the original author of the Python language) 20 guiding principles for Python’s design and posted them on the Python mailing list in 1999.

The Zen of Python is a collection of 19 "guiding principles" for writing computer programs that influence the design of the Python programming language. Peters's list left open the 20th principle "for Guido to fill in", referring to Guido van Rossum. The vacancy for a 20th principle has not been filled.

Peters's Zen of Python was included as entry number 20 in the language's official Python Enhancement Proposals, which was released into the public domain. It is also included as an Easter egg in the Python interpreter, which can be displayed by entering import this. To know more, https://www.python.org/dev/peps/pep-0020/.

2. String Literals and Operations

Literals are notations for constant values of some built-in types. A string literal is where you specify the contents of a string in a program.

Here ‘Attract Positivity’ is a string literal. The variable a is a string variable, or, better put in Python, a variable that points to a string.

String literals can use single or double quote delimiters.

Here, a is a string literal with single quotes and b and c are string literals with double quotes. Let's check the difference between the string literals a,b, and c.

String literals a is with single quote whereas b with double quotes, still there is no difference!

a and b are the same because that is 'A string' but c is 'C string' that is string literal text is different hence a & c and b & c are different.

Literal strings with single quote delimiters can use double quotes inside them without additional efforts.

Some characters preceded by a backslash have special meaning. If you need an actual single quote character inside a literal string delimited by single quotes, you can use the backslash character before the single quote, to tell Python not to terminate the string:

Likewise for double quotes, if you need an actual single quote character inside a literal string delimited by double quotes, you can use the backslash character before the single quote, to tell Python not to terminate the string:

The new line character (\n) in Python is used to mark the end of a line and the beginning of a new line. If you do not give backslash before n then it will treat n as an alphabet and not as an escape character.

If you do not want the backslash to have this special meaning, prefix your string literal with ‘r’, meaning “raw”:

You can use triple quotes to enclose strings with more than one line:

Triple quotes can use single or double quote marks:

A formatted string literal or f-string is a string literal that is prefixed with 'f' or 'F'. These strings may contain replacement fields, which are expressions delimited by curly braces {}. While other string literals always have a constant value, formatted strings are really expressions evaluated at run time.

In nested formatted string literals, firstly inner curly brackets which are '{precision}' and '{width}' will take their values so it will become {value:10.4} and lastly value will take the width of 10 and the precision of 4 to give 12.35.

String literals are described by the following lexical definitions:

Guys, you can find the complete python code for the string literals section at my Github repository link https://github.com/Priyanka-Dandale/Natural-Language-Processing-2/blob/main/M1_NLP_String_Literals_Python.ipynb.

String literals can use single or double quote delimiters. In the below example, the functions id and type give the variable object address and its data type which is 'str' i.e., string in this case.

It’s very easy to use the '+' operator for string concatenation. This operator can be used to add multiple strings together. However, the arguments must be a string.

Note: Strings are immutable, therefore, whenever it is concatenated, it is assigned to a new variable.

You can use triple quotes to enclose strings with more than one line:

Note: Newline escape character \n will be automatically created.

A tab escape character is a backslash \ followed by the t (\t). The \t in the file path "C:\the_folder\new_dir\file.txt" works as a tab escape character.

Using 'r' raw string, by keeping your backslashes in the normal form you can open the file using the given path:

Note`e in the below unicode string literal:

Every emoji has a Unicode associated with it:

Check here for the full list of emoji characters and sequences, CLDR names, and more.

String literals can be concatenated using the '+' operator or just write them in the sequence you want!

String literal and variable can be concatenated using '+' operator:

Some more ways to concatenate string literals in repetition:

A way to concatenate several strings together to make a single string:

Finding substrings in a string using 'in':

Finding length of string using 'len' function:

Let us create a string, verify its data type using 'type' function, find indexes of characters in string using 'enumerate' function and print the characters using respective index and reverse indexing:

Note: Indexes string with 0 from left side and -1 from the right.

Note: string[a:b]: a is included and b is excluded while slicing.

For more on the basics of text processing and analysis using python, go to the link.

Happy Learning! :)

how to
1

About the Creator

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2024 Creatd, Inc. All Rights Reserved.