Strings

A variable is an symbolic name that references a Python object. In this lesson, you will learn about string variables. Feel free to practice with the Python console below:

We can assign a string to a variable like so:

x = "My first variable"
print(x)

Multiline Variables

We can add \n to indicate a new line:

x = "My first variable... \nthis is a new line" 
print(x)

Or we can specify them like so:

x = """
This is a multiline string.
You can have multiple
lines just like this!
"""
print(x)

String Length

When working with strings, it is common to need to limit the length of a string.

x = "My first variable... \nthis is a new line"
len(x)

Strings are arrays!

You can access an index of any string just like an array. The example below will print the first character of the string:

x="This is my string"
print(x[0])

This would be useful if for example if you wanted to do something like take the first and last name of a person and create a username:

firstname = "Joe"
lastname = "Friday"
print(firstname[0] + lastname)

Search Strings

Return true or false if a string contains a keyword:

x = "My first variable... \nthis is a new line"
"variable" in x

We can us if / in to search a string for a keyword:

x = "My first variable... \nthis is a new line"
if "variable" in x:
    print("We found 'variable' in the string!")

Server Academy Members Only

Want to access this lesson? Just sign up for a free Server Academy account and you'll be on your way. Already have an account? Click the Sign Up Free button to get started..

0 0 votes
Lesson Rating
Subscribe
Notify of
profile avatar
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

profile avatar
onanongw(@onanongw)
Member
2 years ago

There’s no more lesson after this?

profile avatar
Ricardo P(@ricardop)
Admin
Reply to  onanongw
2 years ago

Hi profile avatar Onanong Wongjan

These are the only ones for early access to this course. profile avatar Paul Hill is working on more content for it, and we will be adding more videos to the course soon.

Ricardo

profile avatar
onanongw(@onanongw)
Member
Reply to  Ricardo P
2 years ago

Thank you. I like how simple the lessons are and very easy to understand and follow from someone that English is a second languge.

Saving Progress...

Sign up for free!

Sign up for free and get instant access to this course!.

Python 3 for Beginners

0%

0/1 Lessons

Installing Python on Windows

• 1hr 17min

0 / 4 lessons complete

Python Basics

• 28min

0 / 7 lessons complete

Python Variables

• 41min

0 / 8 lessons complete

Even more Python Variables!

• 41min

0 / 6 lessons complete

Conditional Statements

• 15min

0 / 3 lessons complete

Writing Functions

• 30min

0 / 5 lessons complete

Python Loops

• 23min

0 / 5 lessons complete

Python PIP and Modules

• 18min

0 / 4 lessons complete

RegEx

• 26min

0 / 4 lessons complete

Working with APIs

• 12min

0 / 3 lessons complete

Course Conclusion

• 2min

0 / 1 lessons complete