In computer science, sequences of characters are referred to as strings. Strings can be any length and can include any character such as:
- Whitespace (spaces, tabs, new lines)
They are usually contained within a pair of
'single quotes' or
Here are some examples of strings:
message = "Hello, world!"username = "@sonnynomnom"old_password = "Tr0ub4dor&3"new_password = "correcthorsebatterystaple"
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)# Output: Hprint(message)# 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.