String
Anonymous contributor
Published May 5, 2021Updated Feb 24, 2023
Contribute to Docs
In computer science, sequences of characters are referred to as strings. Strings can be any length and can include any character such as:
- Letters
- Numbers
- Symbols
- Whitespace (spaces, tabs, new lines)
They are usually contained within a pair of 'single quotes'
or "double quotes"
.
Here are some examples of strings:
message = "Hello, world!"username = "@sonnynomnom"old_password = "Tr0ub4dor&3"new_password = "correcthorsebatterystaple"
Index
Like any other list, each character in a string has an index that denotes a character’s position.
message = 'Howdy!'
012345
Note: In programming, the index starts from 0, so the index of the first character would be 0.
Finding the Character in a String Given its Index (Python)
In Python, to provide the index of the string message
, add square brackets [
]
to find out the character at that position.
message = 'Hello, world'print(message[0])# Output: Hprint(message[5])# Output: ,
Example of Concatenation (Python)
It is also possible to concatenate strings together using +
in some languages, such as Python and C++. To add a space between strings, an empty space can be put between quotation marks.
Strings in Different Languages
All contributors
- Anonymous contributor
- Christine_Yang
- garanews
- Anonymous contributor
- Anonymous contributor
- christian.dinh
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn more on Codecademy
- Skill path
Code Foundations
Start your programming journey with an introduction to the world of code and basic concepts.Includes 5 CoursesWith CertificateBeginner Friendly4 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours