1.5 KiB
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 aniterable
with a specified string. -
str.replace(old, new, max)
method is used to replace the substringold
with the stringnew
for a total ofmax
times. This method returns a new copy of the string with the replacement, and the originalstr
is unchanged. -
string.split(separator, maxsplit)
Returns a list of substrings delimited by theseparator
, an optionalmaxsplit
number of times, and if not specified, the string will be split on all instances of theseparator
. -
string.strip(to_strip)
Returns a string withto_strip
removed from both the beginning and the end of the string. Ifto_strip
is not specified, this will strip all whitespace characters. -
string.format(to_add)
Returns a formatted string with parameterto_add
placed in the placeholder position. The placeholder has to be defined by a pair of curly braces {} int thestring
's value. The parameter,to_add
can be anything amongst an integer, a floating point numerical, a string or even variables.