String

Christine_Yang269 total contributions
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
- Christine_Yang269 total contributions
- garanews209 total contributions
- Anonymous contributorAnonymous contributor3077 total contributions
- Anonymous contributorAnonymous contributor15 total contributions
- christian.dinh2481 total contributions
- Anonymous contributorAnonymous contributor1 total contribution
- Christine_Yang
- garanews
- Anonymous contributor
- Anonymous contributor
- christian.dinh
- Anonymous contributor
Looking to contribute?
- 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.