Strings in Python
str ="jhdvh"
str=" howuulurh /
hkhrf"
the backslash above wrap text and print the result in a single line
Writing r before a string opening “ treats all the \ as backslash and not as escape sequence.
immutable
indexing 0:4 or -5:-1
print(str[startvalue(included):endvalue(not_included),skip_value])
print("a"+"b")==ab
print("a"*b)==a a a a a ...b times
Functions:
len(str)==gives length of string
str.endwiths("abc")==gives true or false based on if str ends with abc or not
str.count("<<any letter or keyword>>")== returns how many times something appears in str
NOTE :
print("xyyyzxyy". Count("yy")) output will be 2
str.capitalize(empty)==makes first character of the STR capital
str.find("something")== returns position index if present(first occurrence) else -1
str.replace("thing to be replaced","thing replaced with.")-replaces all occurrence
str.strip("string" )==remove "string" from ends, default - empty spaces
str.lstrip()==left strip
str.rstrip()==right strip
str.upper()==
str.lower()
str.isupper()==spaces are allowed
str.islower()==spaces are allowed
str.swapcase()
str.startswith("substr")
str.endswith("substr")
str.index(substring,start,stop)== similar to find but raises error if not found
str.isalpha()==true if all characters are alphabets, blankspace not allowed
str.isalnum()==true if all characters are alphanumeric
str.isdigit()==true if all characters are numbers
str.title()== capitalize each first letter of substring
str.istitle()
str.isspace()=true if only all spaces
str.partition(separator)==splits and created a tuple =substring before , separator,substring after
str.split("some_character",maxsplit)==splits a strings into many at each some_character.
maxsplit =no. of splits default -1==infinite
split = creates a list, if separator is not included then space /newline etc is taken as default
ESCAPE character /n /t /' //(gives single backslash)
ord(alphanumeric)
chr(ascii code)
variable = "separation".join(list) # creates a string with elements of list separated by separation
.join(iterable object),elements should be string
Comments
Post a Comment
For any queries or suggestions
Comment Here