String
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
christian.dinh- Anonymous contributor
Christine_Yang- garanews
- Anonymous contributor
- Anonymous contributor
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 General on Codecademy
- Start your programming journey with an introduction to the world of code and basic concepts.
- Includes 5 Courses
- With Certificate
- Beginner Friendly.4 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours