strings and string manipulation
Strings are fundamental building blocks in programming, allowing us to work with text-based data. They're essential for tasks like user input, output, and text processing. This unit covers string creation, manipulation, and common operations. We'll explore string indexing, slicing, and formatting techniques. You'll learn about built-in string methods, performance considerations, and practical applications of strings in various programming scenarios. Understanding these concepts is crucial for effective text handling in your programs.
'' or double quotes ""len() that returns the number of characters in the string'' or double quotes "" around the desired sequence of characters
my_string = 'Hello, world!' or my_string = "Hello, world!"''' or """ for improved readability
long_string = '''This is a multiline string'''
+ operator to combine them
greeting = 'Hello, ' + 'world!''' or "" to a variable\n for newline and \t for tab to include special charactersr ignore escape characters and treat backslashes as literal characters+ operator
full_name = first_name + ' ' + last_name* operator
repeated_string = 'abc' * 3 results in 'abcabcabc'len() function returns the length of a string, i.e., the number of characters it containslower() method converts all characters in a string to lowercaseupper() method converts all characters in a string to uppercasestrip() method removes leading and trailing whitespace from a stringsplit() method splits a string into a list of substrings based on a specified delimiterjoin() method concatenates a list of strings into a single string using a specified separatorreplace() method replaces occurrences of a substring with another substring[] and zero-based indices
my_string[0] retrieves the first character of my_stringstring[start:end:step]
start is the index where the slice begins (inclusive), defaulting to 0 if omittedend is the index where the slice ends (exclusive), defaulting to the end of the string if omittedstep is the stride or interval between characters, defaulting to 1 if omittedstart and end indexes returns a copy of the original stringstep values reverse the order of the characters in the resulting substringformat() method allows inserting values into a string template using placeholders {}
'Hello, {0}!'.format('Alice') results in 'Hello, Alice!'name = 'Alice' and f'Hello, {name}!' results in 'Hello, Alice!'% operator is an older string formatting technique that uses % placeholders and a tuple of values
'Hello, %s!' % 'Alice' results in 'Hello, Alice!''{:>10.2f}'.format(3.14159) right-aligns the number with a width of 10 and 2 decimal placesin operator
'hello' in 'hello world' returns Truecount() method
'hello world'.count('o') returns 2find() method
'hello world'.find('o') returns 4replace() method
'hello world'.replace('world', 'universe') results in 'hello universe'split() method
'apple,banana,cherry'.split(',') results in ['apple', 'banana', 'cherry']join() method
', '.join(['apple', 'banana', 'cherry']) results in 'apple, banana, cherry'strip(), lstrip(), or rstrip() methods+ operator in loops can be inefficient for large strings
join() method or list comprehension for better performance when building large strings incrementallyformat() or f-stringsio.StringIO for efficient string concatenation in performance-critical code