Introduction
Strings let us work with words and sentences in programs. They help us store and change text like names, messages, or any written info.
Jump into concepts and practice - no test required
Strings let us work with words and sentences in programs. They help us store and change text like names, messages, or any written info.
string_variable = "Hello, world!" # You can use single or double quotes: string1 = 'Hello' string2 = "World" # Multiline strings use triple quotes: multiline = '''This is multiple lines'''
greeting = 'Hi there!' print(greeting)
quote = "She said, \"Hello!\"" print(quote)
poem = '''Roses are red, Violets are blue.''' print(poem)
This program shows how to create a string, add a name inside it, change it to uppercase, and split it into words.
name = "Alice" welcome_message = f"Hello, {name}! Welcome to the program." print(welcome_message) # Changing text shout = welcome_message.upper() print(shout) # Splitting text words = welcome_message.split() print(words)
Use \n inside strings to start a new line.
Strings can be joined with + or repeated with *.
Use len(string) to find out how many characters a string has.
Strings hold text inside quotes.
You can change, join, split, and check strings easily.
Triple quotes help with writing text on many lines.
"Hello" or 'Hello' uses quotes correctly. Parentheses, curly braces, and square brackets are for other data types.'''This is a multi-line string''' uses triple single quotes correctly. Options B and C use single or double quotes for single-line strings. --This is a multi-line string-- is invalid syntax.text = "Hello World" print(text[6:])
name = 'Alice print(name)
words = ['apple', 'banana', 'cherry']. Which code correctly joins them into a single string separated by commas?result = ','.join(words) correctly calls join on the comma string with the list words. Others misuse join or pass wrong arguments.