TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to PYTHON

Python String Literal: F, R Strings

Use string literals with triple quotes and raw string literals. Add and multiply strings.
Literals. In Python programs we often specify strings directly. This is a string literal: a string that is part of the program itself (in quotes).Strings
Triple-quoted. This literal syntax form begins and ends with three quotes. Newlines are left as is. In triple-quoted syntax, we do not need to escape quotes.

Note: In this syntax, the Python interpreter scans for three quotes for the start and end of the literal. A single quote can also be used.

Note 2: We must surround a triple-quoted string with the same kind of quotes. Double quotes must be matched with double quotes.

Python program that uses triple-quotes # Use a triple-quoted string. v = """This string has triple "quotes" on it.""" print(v) Output This string has triple "quotes" on it.
Parentheses. We can place parentheses around string literals on separate lines to combine them into one string literal. This syntax is an alternative to triple-quoted in certain cases.

Usually: It is best to use standard syntax like triple-quoted strings. Here we combine three string literals into one.

Python program that uses parentheses around literals # Surround string literals with parentheses to combine them. lines = ("cat " "bird " "frog") print(lines) Output cat bird frog
Raw literals. By prefixing a string literal with an r, we specify a raw string. In a raw string, the backslash character does not specify an escape sequence—it is a regular character.

Tip: Raw string literals are ideal for regular expression patterns. In "re" we often use the backslash.

Here: The "123" is treated as an escaped sequence in the normal string literal, but not in the raw one.

Paths: For file paths or folder paths, raw string literals are ideal. They eliminate confusion about slashes or backslashes on Windows.

Path
Python program that uses raw string # In a raw string "\" characters do not escape. raw = r"\directory\123" val = "\directory\123" print(raw) print(val) Output \directory\123 \directoryS
Format literal, f. We can prefix a string with the "f" character to specify a format literal. Variables in the current scope are placed inside the curly brackets surrounding their names.format
Python program that uses format literals brick_color = "red" brick_size = 50 # Use format literal. result = f"the brick weighs {brick_size} and is {brick_color}" print(result) Output the brick weighs 50 and is red
Add, multiply. A string can be added to another string. And it can be multiplied by a number. This copies that same string into a new string the specified number of times.

Caution: If you multiply a string against a large number, you may get a MemoryError. An if-check to prevent this may be helpful.

Python program that adds, multiplies strings s = "abc?" # Add two strings together. add = s + s print(add) # Multiply a string. product = s * 3 print(product) Output abc?abc? abc?abc?abc?
A review. String literals are everywhere in Python programs. For file paths, raw literals (with r) are an ideal syntax. We rarely multiply strings, but this too is possible.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf