String
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.