Python Strings

  • In Python, a string is a sequence of characters, enclosed in single quotes (‘…’) or double quotes (“…”).
  • Strings are used for representing text values and are one of the most basic data types in Python.

Below is a table of common string methods:

Method NameDescriptionSyntaxExample
str.upper()Converts all characters in the string to uppercasestr.upper()"car".upper() returns "CAR"
str.lower()Converts all characters in the string to lowercasestr.lower()"CAR".lower() returns "car"
str.capitalize()Converts the first character of the string to uppercase and all other characters to lowercasestr.capitalize()"car".capitalize() returns "Car"
str.title()Converts the first letter of each word in the string to uppercasestr.title()"my first car".title() returns "My First Car"
str.split(sep)Splits the string into a list of strings using a specified separator sepstr.split(sep)"car, bike, truck".split(", ") returns ["car", "bike", "truck"]
str.join(iterable)Joins a list of strings with the string as a separatorsep.join(iterable)", ".join(["car", "bike", "truck"]) returns "car, bike, truck"
str.replace(old, new)Replaces all occurrences of old with new in the stringstr.replace(old, new)"car".replace("c", "b") returns "bar"
str.count(sub)Returns the number of occurrences of sub in the stringstr.count(sub)"car".count("a") returns 1
str.startswith(prefix)Returns True if the string starts with prefix, False otherwisestr.startswith(prefix)"car".startswith("c") returns True
str.endswith(suffix)Returns True if the string ends with suffix, False otherwisestr.endswith(suffix)"car".endswith("r") returns True

More String methods:

Method NameDescriptionSyntaxExample
str.strip()Removes whitespace characters from the beginning and end of the stringstr.strip()" car ".strip() returns "car"
str.lstrip()Removes whitespace characters from the beginning of the stringstr.lstrip()" car".lstrip() returns "car"
str.rstrip()Removes whitespace characters from the end of the stringstr.rstrip()"car ".rstrip() returns "car"
str.find(sub)Returns the lowest index in the string where sub is found, or -1 if not foundstr.find(sub)"car".find("a") returns 1
str.index(sub)Returns the lowest index in the string where sub is found, or raises a ValueError if not foundstr.index(sub)"car".index("a") returns 1
str.isdigit()Returns True if all characters in the string are digits, False otherwisestr.isdigit()"123".isdigit() returns True
str.isalpha()Returns True if all characters in the string are alphabetic, False otherwisestr.isalpha()"abc".isalpha() returns True
str.isalnum()Returns True if all characters in the string are alphanumeric, False otherwisestr.isalnum()"abc123".isalnum() returns True

Python code example:


Uppercase: CAR
Lowercase: car
Stripped: car
Left-stripped: car
Right-stripped: car
‘a’ found at index: 1
‘a’ found at index: 1
Is digit: False
Is alphabetic: True
Is alphanumeric: True

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top