1.5 KiB
title
| title |
|---|
| String Methods |
TODO: string basic info
Creation:
An empty string is created using a pair of quotation marks or apostrophes:
>>> new_string = ''
>>> type(new_string)
<class 'string'>
>>> len(new_string)
0
-
string.find('you')Returns the lowest position that the substring is found at. -
str.join(iterable)Join all elements in aniterablewith a specified string. -
str.replace(old, new, max)method is used to replace the substringoldwith the stringnewfor a total ofmaxtimes. This method returns a new copy of the string with the replacement, and the originalstris unchanged. -
string.split(separator, maxsplit)Returns a list of substrings delimited by theseparator, an optionalmaxsplitnumber of times, and if not specified, the string will be split on all instances of theseparator. -
string.strip(to_strip)Returns a string withto_stripremoved from both the beginning and the end of the string. Ifto_stripis not specified, this will strip all whitespace characters. -
string.format(to_add)Returns a formatted string with parameterto_addplaced in the placeholder position. The placeholder has to be defined by a pair of curly braces {} int thestring's value. The parameter,to_addcan be anything amongst an integer, a floating point numerical, a string or even variables.