You are on page 1of 23

Algoritma dan

Pemrograman

string

Adi
adi4vista@gmail.com
Programming Wisdom

• Programming is not about


typing, it's about thinking.
- Rich Hickey
string
• what is string ?
– basic data type in python
– contains sequences of character data
string manipulation
• string operators
– the + operator
• concatenates strings
• returns a string consisting of the operands joined together
– the * operator
• creates multiple copies of a string
– the in operator
• a membership operator
• return True if the first operand is contained within the
second, and False if otherwise
string manipulation sample
built-in string function
• chr()
– convert an int to a character
• ord()
– convert a character to an int
• len()
– return the length of a string
• str()
– return a string representation of an object
string indexing
• in python, strings are ordered sequences of
character data
• individual characters in a string can be
accessed by specifying the string name
followed by a number in square brackets []
• string indexing in python is zero based
string indexing
string indexing
• String indices can also be specified with
negative numbers, in which case indexing
occurs from the end of the string backward
• -1 refers to the last character, -2 the second-
to-last character, and so on.
string indexing
string slicing
• a form of indexing syntax that extracts
substrings from a string
• an expression of the form s[m:n] returns the
portion of s starting with position m, and up
to but not including position n
interpolation variables into a string
• referensi:
– https://realpython.com/python-f-strings/
• using
– % formatting
• has been in the language since the very beginning
• https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting
– str.format()
• since python 2.6
• https://docs.python.org/3/library/stdtypes.html#str.format
– Template strings (standard library)
– f-strings
• since python 3.6
• https://docs.python.org/3/reference/lexical_analysis.html#f-strings
% formatting
str.format()
Template strings (standard library)
f-strings
• a new and improved way to format strings in
python
• also called “formatted string literals”
built-in string methods
built-in string methods
• s.capitalize()
• s.lower()
• s.swapcase()
• s.title()
• s.upper()
• s.count(sub[, start[, end]])
• s.endswith(suffix[, start[, end]])
• s.find(sub[, start[, end]])
• s.index(sub[, start[, end]])
• s.rfind(sub[, start[, end]])
• s.rindex(sub[, start[, end]])
• s.startswith(prefix[, start[, end]])
built-in string methods
• s.isalnum()
• s.isalpha()
• s.isdigit()
• s.isidentifier()
• s.islower()
• s.isprintable()
• s.isspace()
• s.istitle()
• s.isupper()
• s.strip()
• s.replace(old, new[, count])
built-in string methods
• s.join(<iterable>)
• s.split(sep=None, maxsplit=-1)
• and many more ...

You might also like