You have 3 free guides left 😟
Unlock your guides
You have 3 free guides left 😟
Unlock your guides

1.4 String basics

2 min readjune 24, 2024

Strings are fundamental building blocks Python, allowing you to work with text data. They're created using quotes, can be manipulated with various operations, and have properties like length that you can easily determine.

Understanding basics is crucial for text processing in Python. You'll learn how to create, combine, and modify strings, as well as access individual characters and perform comparisons between different strings.

String Fundamentals

String creation with quotes

Top images from around the web for String creation with quotes
Top images from around the web for String creation with quotes
  • Create strings by enclosing a sequence of characters in single quotes ('') or double quotes ("")
  • Ensure the opening and closing quotation marks match to avoid syntax errors
  • Include special characters in strings using which start with a backslash ($$ followed by a character ( for newline, for tab, \ for backslash, ' for single quote, " for double quote)
  • Examples: 'Hello, world!', "This is a string", 'It's a beautiful day', "She said, "Hello!"", 'Line 1\nLine 2\tTabbed'

Length determination with len()

  • Determine the number of characters in a string using the function which returns an integer value
  • Include all characters in the length count, such as spaces and special characters
  • Examples: len('Hello') returns 5, len('Hello, world!') returns 13, len('') returns 0 for an empty string

String concatenation with +

  • Join two or more strings together using the + operator, creating a new string without modifying the original strings
  • Overload the + operator for strings, causing it to behave differently than when used with numbers
  • Convert non-string values to strings using () before concatenating them with other strings
  • Examples: 'Hello' + ', ' + 'world!' results in 'Hello, world!', 'a' + 'b' + 'c' results in 'abc', 'Number: ' + str(42) results in 'Number: 42'

String operations and representations

  • Access individual characters in a string using , where the first character is at index 0
  • Perform various operations on strings using , such as upper(), lower(), strip(), and split()
  • Compare strings using comparison operators (==, !=, <, >, <=, >=) which use lexicographic ordering based on or character representations
  • ASCII and Unicode are character encoding standards that assign numeric values to characters, allowing computers to represent and manipulate text
© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.

© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.
Glossary
Glossary