C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Python String
See a list of methods that can be used on strings. Review string literal syntax.
String. The forest air is cold. Suddenly an owl appears and perches on a log. It drops a message and flies away. You take the paper and find it has string data on it.
String methods. The mysterious owl most likely does not code Python well. But this should not stop you. Here are some common string methods.asciicapitalizecasefoldcountendswithfindformatinindexisalnumisloweristitleisupperjoinlenljustlowerlstripmaketranspartitionreplacerfindrindexrjustrsplitrstripsplitsplitlinesstartswithstring.ascii_lettersstring.ascii_lowercasestring.ascii_uppercasestring.digitsstring.punctuationstring.whitespacestriptitletranslateupper+ (Operator)* (Operator)== (Operator)!= (Operator)
Literals. In Python a string literal can be triple-quoted—this can make adding newlines easier. A raw literal (r) is useful for Windows paths.Literals: triple-quotedLiterals: f (format)Literals: r (raw)
For-loop, characters. With a for-loop, we can iterate over the characters in a string. In loops we can use "in" or the range() built-in.String: for
Substring. We take substrings with the slice syntax. No substring method exists. We cannot assign a slice in a string, but we can concatenate slices together.String: SubstringString: Right
Ord, chr. Often we have one-character strings. We can convert these strings into integers with ord. And with chr we convert an integer to a 1-char string.ord, chr
Textwrap. This is a helpful module included with Python. Textwrap offers a line-breaking algorithm. This can help with console output.Textwrap
Markup. There is no one way to parse HTML. A popular approach is to use the html.parser module. From it, we access the HTMLParser type. We build a class that inherits from HTMLParser.HTMLHTML: Remove HTML TagsXML
Ciphers. These are one kind of string algorithm. They change the values of characters in a string. One common cipher is the ROT13 algorithm.Cipher: ROT13Cipher: Caesar
Reverse, sort letters. A string can be sorted and reversed. We use list comprehension on the string to get a list of its characters. And then we can use sort() or reverse().Letters: ReverseLetters: Sort
StringIO. This is a buffer that can be used to quickly generate a large string. In my benchmarks, StringIO is faster for appending many strings in a loop.StringIO
ASCII table. In Python (as in all computer languages) we represent letters with numbers. This is a system called ASCII. We can generate an ASCII table with Python.ASCII Table
String handling, lists. Python makes string handling simple. It considers text handling one of its core purposes. Strings are often used in lists.String List
Strings are great. But Python also introduces regular expressions for textual processing. These text expressions are best used for complex pattern-matching.
© TheDeveloperBlog.com