Using string operations
String operations are very common when working with Python and text data. Therefore, this section will review how to initialize a string, string indexing/slicing, and some common string methods.
Note
We will not review string regular expressions, as this is a large topic with significant depth. Check out Mastering Python Regular Expressions by Victor Romero and Felix L. Luis for more instructions on this topic.
Initializing a string
Python allows for string initialization (creation) in several ways. Two ways include single quotes (''
) and double quotes (""
):
# Single quotes s = 'Hello, World!' print(s)Â Â # prints: Hello, World! # Double quotes s = "Hello, World!" print(s)Â Â # prints: Hello, World!
Single and double quotes are basically interchangeable. The only difference comes into play when you have a quote mark (single or double) inside a string. For example, one common scenario is...