strings col1000 python Strings
STRINGS ARE IMMUTABLE SO THERE IS NO FUNCTION THAT CHANGES A STRING IN PLACE
S=S.FUNC() WILL MAKE A NEW STRING IS THE ONLY WAY TO UPDATE THE STRING
1. Case Modification Methods
-
upper()/lower()-
Syntax:
s.upper(),s.lower() -
Explanation: Converts all characters to uppercase or lowercase.
-
-
capitalize()/title()-
Syntax:
s.capitalize(),s.title() -
Explanation: Capitalizes the first character of the string, or the first letter of every word, respectively.
-
-
swapcase()-
Syntax:
s.swapcase() -
Explanation: Inverts the case of all characters (lowercase becomes uppercase and vice versa).
-
2. Searching & Validation Methods
-
find(sub)/count(sub)-
Syntax:
s.find("text"),s.count("text") -
Explanation:
find()returns the lowest index where a substring is found (or-1), whilecount()returns how many times it appears.
-
-
startswith(sub)/endswith(sub)-
Syntax:
s.startswith("A"),s.endswith("Z") -
Explanation: Returns
Trueif the string starts or ends with the specified substring.
-
-
isdigit()/isalpha()/isalnum()-
Syntax:
s.isdigit(),s.isalpha() -
Explanation: Boolean checks to see if characters are purely digits, purely alphabetical, or alphanumeric.
-
3. Modification & Formatting Methods
-
strip()-
Syntax:
s.strip() -
Explanation: Removes leading and trailing whitespace (variants include
lstrip()andrstrip()).
-
-
replace(old, new)-
Syntax:
s= s.replace("old", "new") -
Explanation: Replaces all occurrences of a specified substring with a new one.
-
-
split(sep)/join(iterable)-
Syntax:
s.split(","),",".join(list) -
Explanation:
split()breaks a string into a list based on a separator;join()merges an iterable of strings into a single string using the string as a separator.
-
Note: Similar to lists, strings do not use explicit attributes for metadata, but you can inspect them using built-in functions like
len(s)to get the character count.