Telling Python to Read a String Within Yaml
Watch Now This tutorial has a related video class created by the Real Python squad. Watch it together with the written tutorial to deepen your agreement: Strings and Character Information in Python
In the tutorial on Bones Data Types in Python, you learned how to define strings: objects that comprise sequences of character data. Processing grapheme data is integral to programming. It is a rare application that doesn't need to manipulate strings at least to some extent.
Hither's what you'll learn in this tutorial: Python provides a rich prepare of operators, functions, and methods for working with strings. When you lot are finished with this tutorial, you will know how to admission and extract portions of strings, and likewise be familiar with the methods that are available to manipulate and change string data.
Yous will as well exist introduced to ii other Python objects used to correspond raw byte data, the bytes
and bytearray
types.
String Manipulation
The sections beneath highlight the operators, methods, and functions that are bachelor for working with strings.
String Operators
You have already seen the operators +
and *
applied to numeric operands in the tutorial on Operators and Expressions in Python. These two operators tin can be applied to strings likewise.
The +
Operator
The +
operator concatenates strings. It returns a string consisting of the operands joined together, as shown here:
>>>
>>> s = 'foo' >>> t = 'bar' >>> u = 'baz' >>> south + t 'foobar' >>> s + t + u 'foobarbaz' >>> print ( 'Go team' + '!!!' ) Become squad!!!
The *
Operator
The *
operator creates multiple copies of a string. If s
is a string and n
is an integer, either of the following expressions returns a string consisting of northward
concatenated copies of due south
:
due south * n
north * s
Here are examples of both forms:
>>>
>>> southward = 'foo.' >>> s * 4 'foo.foo.foo.foo.' >>> 4 * s 'foo.foo.foo.foo.'
The multiplier operand n
must be an integer. You'd think it would be required to exist a positive integer, just amusingly, information technology can be zero or negative, in which instance the result is an empty string:
If you were to create a string variable and initialize information technology to the empty string by assigning it the value 'foo' * -8
, anyone would rightly think y'all were a bit daft. But it would piece of work.
The in
Operator
Python as well provides a membership operator that can exist used with strings. The in
operator returns Truthful
if the first operand is contained inside the second, and Simulated
otherwise:
>>>
>>> s = 'foo' >>> s in 'That \' s food for thought.' True >>> south in 'That \' s expert for now.' Imitation
There is also a non in
operator, which does the opposite:
>>>
>>> 'z' not in 'abc' Truthful >>> 'z' non in 'xyz' Faux
Built-in String Functions
As you saw in the tutorial on Basic Data Types in Python, Python provides many functions that are built-in to the interpreter and always available. Here are a few that work with strings:
Part | Description |
---|---|
chr() | Converts an integer to a graphic symbol |
ord() | Converts a grapheme to an integer |
len() | Returns the length of a string |
str() | Returns a cord representation of an object |
These are explored more fully beneath.
ord(c)
Returns an integer value for the given character.
At the most basic level, computers store all data equally numbers. To stand for character data, a translation scheme is used which maps each character to its representative number.
The simplest scheme in common use is chosen ASCII. It covers the common Latin characters y'all are probably about accepted to working with. For these characters, ord(c)
returns the ASCII value for grapheme c
:
>>>
>>> ord ( 'a' ) 97 >>> ord ( '#' ) 35
ASCII is fine as far as it goes. But at that place are many dissimilar languages in use in the world and countless symbols and glyphs that announced in digital media. The total set of characters that potentially may demand to be represented in figurer code far surpasses the ordinary Latin messages, numbers, and symbols y'all commonly see.
Unicode is an aggressive standard that attempts to provide a numeric lawmaking for every possible character, in every possible language, on every possible platform. Python iii supports Unicode extensively, including allowing Unicode characters within strings.
As long every bit you lot stay in the domain of the mutual characters, in that location is petty practical difference between ASCII and Unicode. But the ord()
function will return numeric values for Unicode characters likewise:
>>>
>>> ord ( '€' ) 8364 >>> ord ( '∑' ) 8721
chr(n)
Returns a grapheme value for the given integer.
chr()
does the reverse of ord()
. Given a numeric value n
, chr(n)
returns a cord representing the character that corresponds to due north
:
>>>
>>> chr ( 97 ) 'a' >>> chr ( 35 ) '#'
chr()
handles Unicode characters as well:
>>>
>>> chr ( 8364 ) '€' >>> chr ( 8721 ) '∑'
len(s)
Returns the length of a string.
With len()
, you can check Python string length. len(southward)
returns the number of characters in s
:
>>>
>>> southward = 'I am a string.' >>> len ( s ) 14
str(obj)
Returns a cord representation of an object.
Virtually any object in Python can be rendered as a string. str(obj)
returns the string representation of object obj
:
>>>
>>> str ( 49.2 ) '49.2' >>> str ( 3 + 4 j ) '(3+4j)' >>> str ( 3 + 29 ) '32' >>> str ( 'foo' ) 'foo'
String Indexing
Ofttimes in programming languages, private items in an ordered ready of data tin exist accessed directly using a numeric index or cardinal value. This procedure is referred to every bit indexing.
In Python, strings are ordered sequences of grapheme information, and thus can be indexed in this mode. Individual characters in a string tin can be accessed by specifying the string proper name followed by a number in square brackets ([]
).
Cord indexing in Python is zero-based: the offset graphic symbol in the string has index 0
, the next has index one
, and and so on. The index of the last graphic symbol volition be the length of the string minus 1.
For example, a schematic diagram of the indices of the string 'foobar'
would look similar this:

The private characters tin be accessed by index as follows:
>>>
>>> due south = 'foobar' >>> s [ 0 ] 'f' >>> due south [ 1 ] 'o' >>> s [ 3 ] 'b' >>> len ( southward ) 6 >>> due south [ len ( s ) - ane ] 'r'
Attempting to alphabetize beyond the end of the string results in an error:
>>>
>>> southward [ half-dozen ] Traceback (well-nigh recent call last): File "<pyshell#17>", line ane, in <module> s [ half-dozen ] IndexError: string index out of range
String indices can besides be specified with negative numbers, in which case indexing occurs from the cease of the string backward: -1
refers to the last character, -2
the second-to-last graphic symbol, and and so on. Here is the same diagram showing both the positive and negative indices into the cord 'foobar'
:

Here are some examples of negative indexing:
>>>
>>> s = 'foobar' >>> s [ - ane ] 'r' >>> s [ - 2 ] 'a' >>> len ( s ) 6 >>> s [ - len ( s )] 'f'
Attempting to index with negative numbers beyond the start of the cord results in an error:
>>>
>>> s [ - 7 ] Traceback (most recent telephone call last): File "<pyshell#26>", line one, in <module> southward [ - seven ] IndexError: string alphabetize out of range
For any non-empty string southward
, s[len(s)-1]
and s[-1]
both return the last character. There isn't whatsoever index that makes sense for an empty string.
String Slicing
Python also allows a grade of indexing syntax that extracts substrings from a string, known as string slicing. If southward
is a string, an expression of the form s[m:due north]
returns the portion of s
starting with position grand
, and up to but not including position n
:
>>>
>>> s = 'foobar' >>> southward [ 2 : 5 ] 'oba'
Again, the second index specifies the get-go grapheme that is not included in the result—the character 'r'
(s[5]
) in the example above. That may seem slightly unintuitive, but it produces this effect which makes sense: the expression s[chiliad:n]
will return a substring that is northward - grand
characters in length, in this case, v - 2 = three
.
If y'all omit the first alphabetize, the slice starts at the first of the string. Thus, s[:grand]
and s[0:yard]
are equivalent:
>>>
>>> south = 'foobar' >>> s [: iv ] 'foob' >>> due south [ 0 : 4 ] 'foob'
Similarly, if you omit the second index as in s[n:]
, the slice extends from the first index through the finish of the cord. This is a squeamish, concise alternative to the more cumbersome due south[n:len(s)]
:
>>>
>>> s = 'foobar' >>> s [ 2 :] 'obar' >>> s [ 2 : len ( s )] 'obar'
For any string s
and any integer n
(0 ≤ n ≤ len(s)
), south[:northward] + southward[northward:]
volition exist equal to s
:
>>>
>>> s = 'foobar' >>> s [: 4 ] + southward [ 4 :] 'foobar' >>> south [: iv ] + s [ 4 :] == s True
Omitting both indices returns the original string, in its entirety. Literally. It's not a copy, it's a reference to the original string:
>>>
>>> s = 'foobar' >>> t = s [:] >>> id ( s ) 59598496 >>> id ( t ) 59598496 >>> s is t Truthful
If the first index in a piece is greater than or equal to the second index, Python returns an empty string. This is yet some other obfuscated way to generate an empty string, in case you were looking for 1:
>>>
>>> southward [ ii : 2 ] '' >>> south [ 4 : 2 ] ''
Negative indices tin can exist used with slicing as well. -1
refers to the last character, -2
the 2nd-to-last, and so on, just every bit with simple indexing. The diagram below shows how to slice the substring 'oob'
from the string 'foobar'
using both positive and negative indices:

Here is the corresponding Python code:
>>>
>>> s = 'foobar' >>> s [ - 5 : - ii ] 'oob' >>> s [ 1 : four ] 'oob' >>> s [ - five : - ii ] == southward [ i : 4 ] True
Specifying a Stride in a String Slice
There is one more variant of the slicing syntax to discuss. Calculation an additional :
and a 3rd index designates a stride (also chosen a pace), which indicates how many characters to jump after retrieving each character in the slice.
For case, for the cord 'foobar'
, the slice 0:6:two
starts with the first character and ends with the terminal graphic symbol (the whole string), and every second grapheme is skipped. This is shown in the following diagram:

Similarly, i:six:ii
specifies a slice starting with the second graphic symbol (index 1
) and ending with the terminal character, and again the stride value 2
causes every other grapheme to be skipped:

The illustrative REPL code is shown here:
>>>
>>> due south = 'foobar' >>> s [ 0 : 6 : 2 ] 'foa' >>> south [ one : six : 2 ] 'obr'
Equally with any slicing, the first and 2nd indices can exist omitted, and default to the showtime and final characters respectively:
>>>
>>> south = '12345' * 5 >>> southward '1234512345123451234512345' >>> s [:: five ] '11111' >>> southward [ 4 :: five ] '55555'
You lot can specify a negative pace value besides, in which case Python steps backward through the string. In that example, the starting/first index should be greater than the catastrophe/second alphabetize:
>>>
>>> s = 'foobar' >>> due south [ 5 : 0 : - 2 ] 'rbo'
In the in a higher place example, five:0:-two
ways "start at the last character and step backward past 2
, up to but not including the first character."
When yous are stepping astern, if the first and second indices are omitted, the defaults are reversed in an intuitive manner: the showtime alphabetize defaults to the finish of the string, and the second index defaults to the starting time. Here is an case:
>>>
>>> s = '12345' * 5 >>> s '1234512345123451234512345' >>> south [:: - five ] '55555'
This is a common paradigm for reversing a cord:
>>>
>>> s = 'If Comrade Napoleon says it, information technology must be correct.' >>> s [:: - i ] '.thgir eb tsum ti ,ti syas noelopaN edarmoC fI'
Interpolating Variables Into a Cord
In Python version 3.half dozen, a new string formatting mechanism was introduced. This feature is formally named the Formatted String Literal, but is more usually referred to by its nickname f-string.
The formatting adequacy provided by f-strings is extensive and won't be covered in full detail hither. If you want to learn more, you tin can check out the Existent Python commodity Python three'due south f-Strings: An Improved String Formatting Syntax (Guide). At that place is besides a tutorial on Formatted Output coming upwards later in this series that digs deeper into f-strings.
One simple feature of f-strings you can start using right away is variable interpolation. You can specify a variable name straight within an f-cord literal, and Python volition replace the name with the corresponding value.
For instance, suppose you lot want to display the result of an arithmetic calculation. Yous can do this with a straightforward print()
statement, separating numeric values and string literals past commas:
>>>
>>> n = 20 >>> m = 25 >>> prod = north * m >>> print ( 'The product of' , n , 'and' , m , 'is' , prod ) The production of twenty and 25 is 500
Simply this is cumbersome. To achieve the same thing using an f-string:
- Specify either a lowercase
f
or majusculeF
directly before the opening quote of the string literal. This tells Python it is an f-cord instead of a standard cord. - Specify any variables to exist interpolated in curly braces (
{}
).
Recast using an f-cord, the in a higher place case looks much cleaner:
>>>
>>> due north = 20 >>> m = 25 >>> prod = n * m >>> impress ( f 'The product of { n } and { m } is { prod } ' ) The product of 20 and 25 is 500
Any of Python's three quoting mechanisms can be used to define an f-string:
>>>
>>> var = 'Bark' >>> print ( f 'A canis familiaris says { var } !' ) A domestic dog says Bawl! >>> print ( f "A canis familiaris says { var } !" ) A domestic dog says Bark! >>> impress ( f '''A domestic dog says { var } !''' ) A dog says Bark!
Modifying Strings
In a nutshell, you can't. Strings are one of the data types Python considers immutable, meaning not able to be changed. In fact, all the information types yous have seen so far are immutable. (Python does provide data types that are mutable, as you will presently encounter.)
A statement like this will cause an mistake:
>>>
>>> s = 'foobar' >>> due south [ 3 ] = 'x' Traceback (most recent call last): File "<pyshell#40>", line 1, in <module> s [ 3 ] = 'ten' TypeError: 'str' object does not support item assignment
In truth, in that location really isn't much need to modify strings. You can normally hands accomplish what yous want by generating a copy of the original string that has the desired alter in place. There are very many ways to do this in Python. Here is i possibility:
>>>
>>> south = s [: 3 ] + 'x' + s [ four :] >>> s 'fooxar'
There is also a born cord method to accomplish this:
>>>
>>> s = 'foobar' >>> due south = due south . supercede ( 'b' , 'x' ) >>> s 'fooxar'
Read on for more data near born string methods!
Congenital-in String Methods
You lot learned in the tutorial on Variables in Python that Python is a highly object-oriented linguistic communication. Every particular of information in a Python plan is an object.
You are besides familiar with functions: callable procedures that you can invoke to perform specific tasks.
Methods are similar to functions. A method is a specialized type of callable process that is tightly associated with an object. Like a function, a method is called to perform a singled-out task, but information technology is invoked on a specific object and has cognition of its target object during execution.
The syntax for invoking a method on an object is as follows:
This invokes method .foo()
on object obj
. <args>
specifies the arguments passed to the method (if any).
You will explore much more well-nigh defining and calling methods later in the discussion of object-oriented programming. For now, the goal is to present some of the more commonly used congenital-in methods Python supports for operating on string objects.
In the following method definitions, arguments specified in square brackets ([]
) are optional.
Case Conversion
Methods in this group perform case conversion on the target string.
southward.capitalize()
Capitalizes the target string.
s.capitalize()
returns a copy of s
with the first character converted to uppercase and all other characters converted to lowercase:
>>>
>>> due south = 'foO BaR BAZ quX' >>> s . capitalize () 'Foo bar baz qux'
Non-alphabetic characters are unchanged:
>>>
>>> s = 'foo123#BAR#.' >>> south . capitalize () 'Foo123#bar#.'
s.lower()
Converts alphabetic characters to lowercase.
south.lower()
returns a copy of due south
with all alphabetic characters converted to lowercase:
>>>
>>> 'FOO Bar 123 baz qUX' . lower () 'foo bar 123 baz qux'
s.swapcase()
Swaps case of alphabetic characters.
south.swapcase()
returns a re-create of s
with capital letter alphabetic characters converted to lowercase and vice versa:
>>>
>>> 'FOO Bar 123 baz qUX' . swapcase () 'foo bAR 123 BAZ Qux'
south.title()
Converts the target string to "title case."
s.title()
returns a copy of southward
in which the start alphabetic character of each give-and-take is converted to uppercase and remaining letters are lowercase:
>>>
>>> 'the sunday too rises' . title () 'The Sun Besides Rises'
This method uses a fairly simple algorithm. It does not effort to distinguish between important and unimportant words, and it does non handle apostrophes, possessives, or acronyms gracefully:
>>>
>>> "what's happened to ted's IBM stock?" . title () "What'S Happened To Ted'S Ibm Stock?"
s.upper()
Converts alphabetic characters to uppercase.
s.upper()
returns a copy of s
with all alphabetic characters converted to uppercase:
>>>
>>> 'FOO Bar 123 baz qUX' . upper () 'FOO BAR 123 BAZ QUX'
Notice and Replace
These methods provide various means of searching the target string for a specified substring.
Each method in this grouping supports optional <start>
and <end>
arguments. These are interpreted as for string slicing: the action of the method is restricted to the portion of the target string starting at character position <start>
and proceeding upwardly to but non including character position <end>
. If <first>
is specified only <end>
is not, the method applies to the portion of the target string from <showtime>
through the end of the string.
s.count(<sub>[, <start>[, <end>]])
Counts occurrences of a substring in the target string.
s.count(<sub>)
returns the number of not-overlapping occurrences of substring <sub>
in southward
:
>>>
>>> 'foo goo moo' . count ( 'oo' ) 3
The count is restricted to the number of occurrences within the substring indicated past <start>
and <end>
, if they are specified:
>>>
>>> 'foo goo moo' . count ( 'oo' , 0 , 8 ) 2
s.endswith(<suffix>[, <outset>[, <end>]])
Determines whether the target string ends with a given substring.
s.endswith(<suffix>)
returns True
if southward
ends with the specified <suffix>
and Fake
otherwise:
>>>
>>> 'foobar' . endswith ( 'bar' ) True >>> 'foobar' . endswith ( 'baz' ) False
The comparing is restricted to the substring indicated by <start>
and <end>
, if they are specified:
>>>
>>> 'foobar' . endswith ( 'oob' , 0 , iv ) True >>> 'foobar' . endswith ( 'oob' , 2 , iv ) False
s.find(<sub>[, <start>[, <end>]])
Searches the target cord for a given substring.
Y'all can use .find()
to see if a Python string contains a particular substring. due south.find(<sub>)
returns the everyman index in southward
where substring <sub>
is found:
>>>
>>> 'foo bar foo baz foo qux' . find ( 'foo' ) 0
This method returns -1
if the specified substring is not found:
>>>
>>> 'foo bar foo baz foo qux' . find ( 'grault' ) -1
The search is restricted to the substring indicated by <commencement>
and <end>
, if they are specified:
>>>
>>> 'foo bar foo baz foo qux' . find ( 'foo' , 4 ) 8 >>> 'foo bar foo baz foo qux' . find ( 'foo' , 4 , 7 ) -1
s.index(<sub>[, <start>[, <end>]])
Searches the target cord for a given substring.
This method is identical to .find()
, except that information technology raises an exception if <sub>
is not found rather than returning -i
:
>>>
>>> 'foo bar foo baz foo qux' . index ( 'grault' ) Traceback (nigh contempo call last): File "<pyshell#0>", line 1, in <module> 'foo bar foo baz foo qux' . index ( 'grault' ) ValueError: substring not found
southward.rfind(<sub>[, <start>[, <cease>]])
Searches the target string for a given substring starting at the finish.
southward.rfind(<sub>)
returns the highest index in s
where substring <sub>
is found:
>>>
>>> 'foo bar foo baz foo qux' . rfind ( 'foo' ) xvi
Equally with .find()
, if the substring is non establish, -i
is returned:
>>>
>>> 'foo bar foo baz foo qux' . rfind ( 'grault' ) -1
The search is restricted to the substring indicated past <beginning>
and <end>
, if they are specified:
>>>
>>> 'foo bar foo baz foo qux' . rfind ( 'foo' , 0 , 14 ) 8 >>> 'foo bar foo baz foo qux' . rfind ( 'foo' , 10 , 14 ) -1
s.rindex(<sub>[, <start>[, <end>]])
Searches the target cord for a given substring starting at the finish.
This method is identical to .rfind()
, except that it raises an exception if <sub>
is not found rather than returning -1
:
>>>
>>> 'foo bar foo baz foo qux' . rindex ( 'grault' ) Traceback (about recent call last): File "<pyshell#1>", line 1, in <module> 'foo bar foo baz foo qux' . rindex ( 'grault' ) ValueError: substring not found
due south.startswith(<prefix>[, <start>[, <cease>]])
Determines whether the target cord starts with a given substring.
When you utilise the Python .startswith()
method, s.startswith(<suffix>)
returns True
if due south
starts with the specified <suffix>
and Imitation
otherwise:
>>>
>>> 'foobar' . startswith ( 'foo' ) True >>> 'foobar' . startswith ( 'bar' ) False
The comparison is restricted to the substring indicated past <start>
and <stop>
, if they are specified:
>>>
>>> 'foobar' . startswith ( 'bar' , 3 ) True >>> 'foobar' . startswith ( 'bar' , three , 2 ) False
Character Classification
Methods in this grouping classify a cord based on the characters it contains.
due south.isalnum()
Determines whether the target string consists of alphanumeric characters.
s.isalnum()
returns True
if due south
is nonempty and all its characters are alphanumeric (either a letter or a number), and False
otherwise:
>>>
>>> 'abc123' . isalnum () True >>> 'abc$123' . isalnum () False >>> '' . isalnum () False
s.isalpha()
Determines whether the target string consists of alphabetic characters.
s.isalpha()
returns True
if s
is nonempty and all its characters are alphabetic, and False
otherwise:
>>>
>>> 'ABCabc' . isalpha () True >>> 'abc123' . isalpha () Simulated
south.isdigit()
Determines whether the target cord consists of digit characters.
You can use the .isdigit()
Python method to check if your string is made of just digits. s.isdigit()
returns True
if s
is nonempty and all its characters are numeric digits, and False
otherwise:
>>>
>>> '123' . isdigit () Truthful >>> '123abc' . isdigit () False
s.isidentifier()
Determines whether the target string is a valid Python identifier.
s.isidentifier()
returns True
if s
is a valid Python identifier co-ordinate to the language definition, and False
otherwise:
>>>
>>> 'foo32' . isidentifier () True >>> '32foo' . isidentifier () Fake >>> 'foo$32' . isidentifier () False
due south.islower()
Determines whether the target string'south alphabetic characters are lowercase.
s.islower()
returns True
if s
is nonempty and all the alphabetic characters it contains are lowercase, and Faux
otherwise. Non-alphabetic characters are ignored:
>>>
>>> 'abc' . islower () Truthful >>> 'abc1$d' . islower () True >>> 'Abc1$D' . islower () False
southward.isprintable()
Determines whether the target string consists entirely of printable characters.
s.isprintable()
returns True
if s
is empty or all the alphabetic characters it contains are printable. Information technology returns Imitation
if southward
contains at to the lowest degree 1 non-printable grapheme. Not-alphabetic characters are ignored:
>>>
>>> 'a \t b' . isprintable () Imitation >>> 'a b' . isprintable () Truthful >>> '' . isprintable () Truthful >>> 'a \northward b' . isprintable () False
south.isspace()
Determines whether the target cord consists of whitespace characters.
s.isspace()
returns Truthful
if s
is nonempty and all characters are whitespace characters, and False
otherwise.
The nigh commonly encountered whitespace characters are space ' '
, tab '\t'
, and newline '\n'
:
>>>
>>> ' \t \n ' . isspace () Truthful >>> ' a ' . isspace () False
Nonetheless, at that place are a few other ASCII characters that qualify equally whitespace, and if yous account for Unicode characters, at that place are quite a few beyond that:
>>>
>>> ' \f\u2005\r ' . isspace () True
('\f'
and '\r'
are the escape sequences for the ASCII Class Feed and Railroad vehicle Return characters; '\u2005'
is the escape sequence for the Unicode Four-Per-Em Space.)
s.istitle()
Determines whether the target string is title cased.
southward.istitle()
returns Truthful
if s
is nonempty, the first alphabetic character of each word is capital letter, and all other alphabetic characters in each word are lowercase. It returns False
otherwise:
>>>
>>> 'This Is A Title' . istitle () Truthful >>> 'This is a title' . istitle () False >>> 'Give Me The #$#@ Ball!' . istitle () Truthful
s.isupper()
Determines whether the target string's alphabetic characters are uppercase.
due south.isupper()
returns Truthful
if s
is nonempty and all the alphabetic characters it contains are uppercase, and False
otherwise. Non-alphabetic characters are ignored:
>>>
>>> 'ABC' . isupper () True >>> 'ABC1$D' . isupper () Truthful >>> 'Abc1$D' . isupper () False
String Formatting
Methods in this group modify or enhance the format of a cord.
s.centre(<width>[, <make full>])
Centers a string in a field.
due south.center(<width>)
returns a string consisting of s
centered in a field of width <width>
. By default, padding consists of the ASCII infinite character:
>>>
>>> 'foo' . heart ( 10 ) ' foo '
If the optional <fill>
argument is specified, it is used as the padding character:
>>>
>>> 'bar' . middle ( ten , '-' ) '---bar----'
If s
is already at least as long as <width>
, it is returned unchanged:
>>>
>>> 'foo' . center ( two ) 'foo'
southward.expandtabs(tabsize=8)
Expands tabs in a string.
s.expandtabs()
replaces each tab graphic symbol ('\t'
) with spaces. By default, spaces are filled in assuming a tab cease at every eighth column:
>>>
>>> 'a \t b \t c' . expandtabs () 'a b c' >>> 'aaa \t bbb \t c' . expandtabs () 'aaa bbb c'
tabsize
is an optional keyword parameter specifying alternate tab end columns:
>>>
>>> 'a \t b \t c' . expandtabs ( 4 ) 'a b c' >>> 'aaa \t bbb \t c' . expandtabs ( tabsize = 4 ) 'aaa bbb c'
s.ljust(<width>[, <fill up>])
Left-justifies a string in field.
s.ljust(<width>)
returns a string consisting of south
left-justified in a field of width <width>
. By default, padding consists of the ASCII space character:
>>>
>>> 'foo' . ljust ( 10 ) 'foo '
If the optional <fill>
argument is specified, it is used as the padding graphic symbol:
>>>
>>> 'foo' . ljust ( 10 , '-' ) 'foo-------'
If s
is already at least as long equally <width>
, it is returned unchanged:
>>>
>>> 'foo' . ljust ( 2 ) 'foo'
due south.lstrip([<chars>])
Trims leading characters from a string.
s.lstrip()
returns a copy of due south
with whatsoever whitespace characters removed from the left end:
>>>
>>> ' foo bar baz ' . lstrip () 'foo bar baz ' >>> ' \t\n foo \t\n bar \t\n baz' . lstrip () 'foo\t\nbar\t\nbaz'
If the optional <chars>
argument is specified, it is a string that specifies the set of characters to be removed:
>>>
>>> 'http://world wide web.realpython.com' . lstrip ( '/:pth' ) 'www.realpython.com'
southward.replace(<former>, <new>[, <count>])
Replaces occurrences of a substring inside a cord.
In Python, to remove a character from a cord, y'all tin use the Python string .supervene upon()
method. southward.replace(<old>, <new>)
returns a copy of southward
with all occurrences of substring <former>
replaced by <new>
:
>>>
>>> 'foo bar foo baz foo qux' . supervene upon ( 'foo' , 'grault' ) 'grault bar grault baz grault qux'
If the optional <count>
statement is specified, a maximum of <count>
replacements are performed, starting at the left stop of due south
:
>>>
>>> 'foo bar foo baz foo qux' . supersede ( 'foo' , 'grault' , two ) 'grault bar grault baz foo qux'
s.rjust(<width>[, <fill up>])
Right-justifies a cord in a field.
s.rjust(<width>)
returns a string consisting of south
correct-justified in a field of width <width>
. Past default, padding consists of the ASCII space graphic symbol:
>>>
>>> 'foo' . rjust ( 10 ) ' foo'
If the optional <fill>
argument is specified, it is used as the padding character:
>>>
>>> 'foo' . rjust ( 10 , '-' ) '-------foo'
If south
is already at to the lowest degree equally long every bit <width>
, it is returned unchanged:
>>>
>>> 'foo' . rjust ( 2 ) 'foo'
s.rstrip([<chars>])
Trims trailing characters from a string.
s.rstrip()
returns a copy of s
with whatsoever whitespace characters removed from the correct end:
>>>
>>> ' foo bar baz ' . rstrip () ' foo bar baz' >>> 'foo \t\due north bar \t\due north baz \t\n ' . rstrip () 'foo\t\nbar\t\nbaz'
If the optional <chars>
argument is specified, it is a string that specifies the set up of characters to exist removed:
>>>
>>> 'foo.$$$;' . rstrip ( ';$.' ) 'foo'
s.strip([<chars>])
Strips characters from the left and right ends of a string.
s.strip()
is essentially equivalent to invoking due south.lstrip()
and due south.rstrip()
in succession. Without the <chars>
argument, it removes leading and abaft whitespace:
>>>
>>> due south = ' foo bar baz \t\t\t ' >>> south = southward . lstrip () >>> s = south . rstrip () >>> s 'foo bar baz'
As with .lstrip()
and .rstrip()
, the optional <chars>
argument specifies the set up of characters to be removed:
>>>
>>> 'world wide web.realpython.com' . strip ( 'due west.moc' ) 'realpython'
s.zfill(<width>)
Pads a string on the left with zeros.
s.zfill(<width>)
returns a copy of s
left-padded with '0'
characters to the specified <width>
:
>>>
>>> '42' . zfill ( 5 ) '00042'
If s
contains a leading sign, it remains at the left edge of the outcome string later zeros are inserted:
>>>
>>> '+42' . zfill ( viii ) '+0000042' >>> '-42' . zfill ( 8 ) '-0000042'
If s
is already at least every bit long as <width>
, information technology is returned unchanged:
>>>
>>> '-42' . zfill ( 3 ) '-42'
.zfill()
is most useful for string representations of numbers, merely Python will nonetheless happily zero-pad a string that isn't:
>>>
>>> 'foo' . zfill ( 6 ) '000foo'
Converting Between Strings and Lists
Methods in this group catechumen between a string and some composite information type by either pasting objects together to make a cord, or by breaking a cord up into pieces.
These methods operate on or return iterables, the full general Python term for a sequential collection of objects. You lot will explore the inner workings of iterables in much more detail in the upcoming tutorial on definite iteration.
Many of these methods render either a listing or a tuple. These are two similar composite data types that are prototypical examples of iterables in Python. They are covered in the side by side tutorial, and then you're most to learn virtually them soon! Until then, merely think of them equally sequences of values. A listing is enclosed in foursquare brackets ([]
), and a tuple is enclosed in parentheses (()
).
With that introduction, let's accept a look at this last group of cord methods.
s.join(<iterable>)
Concatenates strings from an iterable.
southward.bring together(<iterable>)
returns the string that results from concatenating the objects in <iterable>
separated by due south
.
Note that .bring together()
is invoked on s
, the separator cord. <iterable>
must be a sequence of cord objects equally well.
Some sample code should help analyze. In the following example, the separator s
is the string ', '
, and <iterable>
is a list of string values:
>>>
>>> ', ' . join ([ 'foo' , 'bar' , 'baz' , 'qux' ]) 'foo, bar, baz, qux'
The outcome is a single cord consisting of the list objects separated by commas.
In the side by side example, <iterable>
is specified equally a unmarried cord value. When a cord value is used as an iterable, it is interpreted as a listing of the cord's individual characters:
>>>
>>> list ( 'corge' ) ['c', 'o', 'r', 'g', 'e'] >>> ':' . join ( 'corge' ) 'c:o:r:thou:eastward'
Thus, the result of ':'.bring together('corge')
is a string consisting of each graphic symbol in 'corge'
separated by ':'
.
This example fails because one of the objects in <iterable>
is non a string:
>>>
>>> '---' . join ([ 'foo' , 23 , 'bar' ]) Traceback (almost contempo call concluding): File "<pyshell#0>", line 1, in <module> '---' . join ([ 'foo' , 23 , 'bar' ]) TypeError: sequence detail i: expected str case, int found
That can be remedied, though:
>>>
>>> '---' . bring together ([ 'foo' , str ( 23 ), 'bar' ]) 'foo---23---bar'
As y'all will soon see, many composite objects in Python can exist construed as iterables, and .join()
is particularly useful for creating strings from them.
south.partition(<sep>)
Divides a string based on a separator.
s.sectionalization(<sep>)
splits s
at the showtime occurrence of string <sep>
. The return value is a three-part tuple consisting of:
- The portion of
south
preceding<sep>
-
<sep>
itself - The portion of
s
following<sep>
Here are a couple examples of .partition()
in action:
>>>
>>> 'foo.bar' . partition ( '.' ) ('foo', '.', 'bar') >>> 'foo@@bar@@baz' . partition ( '@@' ) ('foo', '@@', 'bar@@baz')
If <sep>
is not establish in south
, the returned tuple contains south
followed past two empty strings:
>>>
>>> 'foo.bar' . partition ( '@@' ) ('foo.bar', '', '')
s.rpartition(<sep>)
Divides a string based on a separator.
due south.rpartition(<sep>)
functions exactly like s.partitioning(<sep>)
, except that due south
is split at the last occurrence of <sep>
instead of the first occurrence:
>>>
>>> 'foo@@bar@@baz' . partition ( '@@' ) ('foo', '@@', 'bar@@baz') >>> 'foo@@bar@@baz' . rpartition ( '@@' ) ('foo@@bar', '@@', 'baz')
southward.rsplit(sep=None, maxsplit=-one)
Splits a cord into a list of substrings.
Without arguments, s.rsplit()
splits s
into substrings delimited by whatever sequence of whitespace and returns the substrings as a list:
>>>
>>> 'foo bar baz qux' . rsplit () ['foo', 'bar', 'baz', 'qux'] >>> 'foo \n\t bar baz \r\f qux' . rsplit () ['foo', 'bar', 'baz', 'qux']
If <sep>
is specified, it is used as the delimiter for splitting:
>>>
>>> 'foo.bar.baz.qux' . rsplit ( sep = '.' ) ['foo', 'bar', 'baz', 'qux']
(If <sep>
is specified with a value of None
, the string is split delimited past whitespace, just as though <sep>
had not been specified at all.)
When <sep>
is explicitly given as a delimiter, consecutive delimiters in south
are assumed to delimit empty strings, which will be returned:
>>>
>>> 'foo...bar' . rsplit ( sep = '.' ) ['foo', '', '', 'bar']
This is not the case when <sep>
is omitted, however. In that case, consecutive whitespace characters are combined into a single delimiter, and the resulting list will never contain empty strings:
>>>
>>> 'foo \t\t\t bar' . rsplit () ['foo', 'bar']
If the optional keyword parameter <maxsplit>
is specified, a maximum of that many splits are performed, starting from the right end of s
:
>>>
>>> 'www.realpython.com' . rsplit ( sep = '.' , maxsplit = i ) ['www.realpython', 'com']
The default value for <maxsplit>
is -1
, which means all possible splits should be performed—the same as if <maxsplit>
is omitted entirely:
>>>
>>> 'www.realpython.com' . rsplit ( sep = '.' , maxsplit =- i ) ['www', 'realpython', 'com'] >>> 'www.realpython.com' . rsplit ( sep = '.' ) ['www', 'realpython', 'com']
s.dissever(sep=None, maxsplit=-1)
Splits a string into a list of substrings.
s.dissever()
behaves exactly like s.rsplit()
, except that if <maxsplit>
is specified, splits are counted from the left cease of s
rather than the right cease:
>>>
>>> 'www.realpython.com' . divide ( '.' , maxsplit = one ) ['www', 'realpython.com'] >>> 'www.realpython.com' . rsplit ( '.' , maxsplit = 1 ) ['www.realpython', 'com']
If <maxsplit>
is not specified, .split()
and .rsplit()
are indistinguishable.
south.splitlines([<keepends>])
Breaks a string at line boundaries.
southward.splitlines()
splits s
up into lines and returns them in a list. Any of the following characters or character sequences is considered to found a line boundary:
Escape Sequence | Character |
---|---|
\n | Newline |
\r | Carriage Return |
\r\n | Carriage Return + Line Feed |
\v or \x0b | Line Tabulation |
\f or \x0c | Form Feed |
\x1c | File Separator |
\x1d | Group Separator |
\x1e | Record Separator |
\x85 | Side by side Line (C1 Control Code) |
\u2028 | Unicode Line Separator |
\u2029 | Unicode Paragraph Separator |
Here is an instance using several dissimilar line separators:
>>>
>>> 'foo \due north bar \r\northward baz \f qux \u2028 quux' . splitlines () ['foo', 'bar', 'baz', 'qux', 'quux']
If consecutive line boundary characters are present in the string, they are assumed to delimit bare lines, which volition appear in the consequence list:
>>>
>>> 'foo \f\f\f bar' . splitlines () ['foo', '', '', 'bar']
If the optional <keepends>
argument is specified and is truthy, and then the lines boundaries are retained in the consequence strings:
>>>
>>> 'foo \n bar \north baz \northward qux' . splitlines ( Truthful ) ['foo\n', 'bar\n', 'baz\north', 'qux'] >>> 'foo \n bar \n baz \n qux' . splitlines ( 1 ) ['foo\n', 'bar\northward', 'baz\north', 'qux']
bytes
Objects
The bytes
object is one of the cadre built-in types for manipulating binary data. A bytes
object is an immutable sequence of unmarried byte values. Each element in a bytes
object is a small-scale integer in the range 0
to 255
.
Defining a Literal bytes
Object
A bytes
literal is defined in the aforementioned way as a cord literal with the addition of a 'b'
prefix:
>>>
>>> b = b 'foo bar baz' >>> b b'foo bar baz' >>> type ( b ) <class 'bytes'>
As with strings, you can employ any of the single, double, or triple quoting mechanisms:
>>>
>>> b 'Contains embedded "double" quotes' b'Contains embedded "double" quotes' >>> b "Contains embedded 'unmarried' quotes" b"Contains embedded 'single' quotes" >>> b '''Contains embedded "double" and 'single' quotes''' b'Contains embedded "double" and \'single\' quotes' >>> b """Contains embedded "double" and 'single' quotes""" b'Contains embedded "double" and \'unmarried\' quotes'
Only ASCII characters are immune in a bytes
literal. Any graphic symbol value greater than 127
must be specified using an appropriate escape sequence:
>>>
>>> b = b 'foo \xdd bar' >>> b b'foo\xddbar' >>> b [ three ] 221 >>> int ( 0xdd ) 221
The 'r'
prefix may exist used on a bytes
literal to disable processing of escape sequences, as with strings:
>>>
>>> b = rb 'foo\xddbar' >>> b b'foo\\xddbar' >>> b [ three ] 92 >>> chr ( 92 ) '\\'
Defining a bytes
Object With the Built-in bytes()
Role
The bytes()
role also creates a bytes
object. What sort of bytes
object gets returned depends on the statement(south) passed to the function. The possible forms are shown below.
bytes(<s>, <encoding>)
Creates a
bytes
object from a string.
bytes(<due south>, <encoding>)
converts cord <southward>
to a bytes
object, using str.encode()
according to the specified <encoding>
:
>>>
>>> b = bytes ( 'foo.bar' , 'utf8' ) >>> b b'foo.bar' >>> type ( b ) <course 'bytes'>
bytes(<size>)
Creates a
bytes
object consisting of nothing (0x00
) bytes.
bytes(<size>)
defines a bytes
object of the specified <size>
, which must be a positive integer. The resulting bytes
object is initialized to null (0x00
) bytes:
>>>
>>> b = bytes ( 8 ) >>> b b'\x00\x00\x00\x00\x00\x00\x00\x00' >>> blazon ( b ) <class 'bytes'>
bytes(<iterable>)
Creates a
bytes
object from an iterable.
bytes(<iterable>)
defines a bytes
object from the sequence of integers generated by <iterable>
. <iterable>
must exist an iterable that generates a sequence of integers n
in the range 0 ≤ n ≤ 255
:
>>>
>>> b = bytes ([ 100 , 102 , 104 , 106 , 108 ]) >>> b b'dfhjl' >>> type ( b ) <course 'bytes'> >>> b [ 2 ] 104
Operations on bytes
Objects
Similar strings, bytes
objects support the common sequence operations:
-
The
in
andnot in
operators:>>>
>>> b = b 'abcde' >>> b 'cd' in b Truthful >>> b 'foo' non in b True
-
The concatenation (
+
) and replication (*
) operators:>>>
>>> b = b 'abcde' >>> b + b 'fghi' b'abcdefghi' >>> b * 3 b'abcdeabcdeabcde'
-
Indexing and slicing:
>>>
>>> b = b 'abcde' >>> b [ two ] 99 >>> b [ 1 : 3 ] b'bc'
-
Built-in functions:
>>>
>>> len ( b ) 5 >>> min ( b ) 97 >>> max ( b ) 101
Many of the methods defined for string objects are valid for bytes
objects too:
>>>
>>> b = b 'foo,bar,foo,baz,foo,qux' >>> b . count ( b 'foo' ) iii >>> b . endswith ( b 'qux' ) True >>> b . find ( b 'baz' ) 12 >>> b . separate ( sep = b ',' ) [b'foo', b'bar', b'foo', b'baz', b'foo', b'qux'] >>> b . centre ( xxx , b '-' ) b'---foo,bar,foo,baz,foo,qux----'
Notice, notwithstanding, that when these operators and methods are invoked on a bytes
object, the operand and arguments must be bytes
objects also:
>>>
>>> b = b 'foo.bar' >>> b + '.baz' Traceback (almost contempo call last): File "<pyshell#72>", line i, in <module> b + '.baz' TypeError: can't concat bytes to str >>> b + b '.baz' b'foo.bar.baz' >>> b . split ( sep = '.' ) Traceback (most contempo call final): File "<pyshell#74>", line ane, in <module> b . split ( sep = '.' ) TypeError: a bytes-like object is required, non 'str' >>> b . separate ( sep = b '.' ) [b'foo', b'bar']
Although a bytes
object definition and representation is based on ASCII text, it actually behaves similar an immutable sequence of small integers in the range 0
to 255
, inclusive. That is why a single element from a bytes
object is displayed equally an integer:
>>>
>>> b = b 'foo \xdd bar' >>> b [ three ] 221 >>> hex ( b [ three ]) '0xdd' >>> min ( b ) 97 >>> max ( b ) 221
A slice is displayed as a bytes
object though, even if it is simply one byte long:
You lot tin can catechumen a bytes
object into a listing of integers with the built-in listing()
function:
>>>
>>> listing ( b ) [97, 98, 99, 100, 101]
Hexadecimal numbers are oftentimes used to specify binary information because two hexadecimal digits correspond directly to a unmarried byte. The bytes
form supports two additional methods that facilitate conversion to and from a string of hexadecimal digits.
bytes.fromhex(<s>)
Returns a
bytes
object synthetic from a cord of hexadecimal values.
bytes.fromhex(<s>)
returns the bytes
object that results from converting each pair of hexadecimal digits in <s>
to the corresponding byte value. The hexadecimal digit pairs in <southward>
may optionally exist separated by whitespace, which is ignored:
>>>
>>> b = bytes . fromhex ( ' aa 68 4682cc ' ) >>> b b'\xaahF\x82\xcc' >>> list ( b ) [170, 104, 70, 130, 204]
b.hex()
Returns a string of hexadecimal value from a
bytes
object.
b.hex()
returns the effect of converting bytes
object b
into a string of hexadecimal digit pairs. That is, information technology does the contrary of .fromhex()
:
>>>
>>> b = bytes . fromhex ( ' aa 68 4682cc ' ) >>> b b'\xaahF\x82\xcc' >>> b . hex () 'aa684682cc' >>> type ( b . hex ()) <form 'str'>
bytearray
Objects
Python supports another binary sequence type called the bytearray
. bytearray
objects are very like bytes
objects, despite some differences:
-
There is no dedicated syntax congenital into Python for defining a
bytearray
literal, like the'b'
prefix that may be used to ascertain abytes
object. Abytearray
object is ever created using thebytearray()
built-in function:>>>
>>> ba = bytearray ( 'foo.bar.baz' , 'UTF-viii' ) >>> ba bytearray(b'foo.bar.baz') >>> bytearray ( 6 ) bytearray(b'\x00\x00\x00\x00\x00\x00') >>> bytearray ([ 100 , 102 , 104 , 106 , 108 ]) bytearray(b'dfhjl')
-
bytearray
objects are mutable. You can modify the contents of abytearray
object using indexing and slicing:>>>
>>> ba = bytearray ( 'foo.bar.baz' , 'UTF-8' ) >>> ba bytearray(b'foo.bar.baz') >>> ba [ 5 ] = 0xee >>> ba bytearray(b'foo.b\xeer.baz') >>> ba [ 8 : xi ] = b 'qux' >>> ba bytearray(b'foo.b\xeer.qux')
A bytearray
object may be constructed directly from a bytes
object also:
>>>
>>> ba = bytearray ( b 'foo' ) >>> ba bytearray(b'foo')
Conclusion
This tutorial provided an in-depth look at the many dissimilar mechanisms Python provides for string treatment, including cord operators, built-in functions, indexing, slicing, and congenital-in methods. Yous also were introduced to the bytes
and bytearray
types.
These types are the first types you have examined that are blended—congenital from a collection of smaller parts. Python provides several composite built-in types. In the adjacent tutorial, you will explore two of the nearly oft used: lists and tuples.
Lookout Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your agreement: Strings and Graphic symbol Information in Python
haislippanytherry40.blogspot.com
Source: https://realpython.com/python-strings/
إرسال تعليق for "Telling Python to Read a String Within Yaml"