Book Image

Sage Beginner's Guide

By : Craig Finch
1 (1)
Book Image

Sage Beginner's Guide

1 (1)
By: Craig Finch

Overview of this book

Table of Contents (17 chapters)
Sage Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – using strings


Let's practice with strings:

string_1 = 'Single quoted string'
string_2 = "Sometimes it's good to use double quotes"

multiline_string = """    This string
    contains single quotes ' and double quotes "
    and spans multiple lines"""

print(string_1)
print(string_2)
print(multiline_string)

numerical_value = 1.616233
print('The value is ' + str(numerical_value))

The result should look like this:

What just happened?

A string literal is an arbitrary sequence of characters enclosed in quotation marks, such as 'Single quoted string' in the example above. String literals can be assigned to variables, like any other type. Single or double quotes can be used. If you need to use a single quote within the string, you need to enclose the string in double quotes, as we did with the string literal assigned to the variable string_2. Enclosing a string in triple quotes (either single or double) allows you to include newlines and quotation marks in the string. We used triple...