Cara menggunakan string object in python

In the tutorial on Basic Data Types in Python, you learned how to define strings: objects that contain sequences of character data. Processing character data is integral to programming. It is a rare application that doesn’t need to manipulate strings at least to some extent.

Here’s what you’ll learn in this tutorial: Python provides a rich set of operators, functions, and methods for working with strings. When you are finished with this tutorial, you will know how to access and extract portions of strings, and also be familiar with the methods that are available to manipulate and modify string data.

You will also be introduced to two other Python objects used to represent raw byte data, the

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
29 and
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
30 types.

Take the Quiz: Test your knowledge with our interactive “Python Strings and Character Data” quiz. Upon completion you will receive a score so you can track your learning progress over time:

Take the Quiz »

String Manipulation

The sections below highlight the operators, methods, and functions that are available for working with strings.

Remove ads

String Operators

You have already seen the operators

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
31 and
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
32 applied to numeric operands in the tutorial on Operators and Expressions in Python. These two operators can be applied to strings as well.

The
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
31 Operator

The

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
31 operator concatenates strings. It returns a string consisting of the operands joined together, as shown here:

>>>

>>> s = 'foo'
>>> t = 'bar'
>>> u = 'baz'

>>> s + t
'foobar'
>>> s + t + u
'foobarbaz'

>>> print('Go team' + '!!!')
Go team!!!

The
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
32 Operator

The

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
32 operator creates multiple copies of a string. If
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 is a string and
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
38 is an integer, either of the following expressions returns a string consisting of
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
38 concatenated copies of
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37:

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
41
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
42

Here are examples of both forms:

>>>

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'

The multiplier operand

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
38 must be an integer. You’d think it would be required to be a positive integer, but amusingly, it can be zero or negative, in which case the result is an empty string:

>>>

>>> 'foo' * -8
''

If you were to create a string variable and initialize it to the empty string by assigning it the value

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
44, anyone would rightly think you were a bit daft. But it would work.

The
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
45 Operator

Python also provides a membership operator that can be used with strings. The

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
45 operator returns
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
47 if the first operand is contained within the second, and
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
48 otherwise:

>>>

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False

There is also a , which does the opposite:

>>>

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False

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:

FunctionDescription

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
50Converts an integer to a character
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
51Converts a character to an integer
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
52Returns the length of a string
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
53Returns a string representation of an object

These are explored more fully below.

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
54

Returns an integer value for the given character.

At the most basic level, computers store all information as numbers. To represent character data, a translation scheme is used which maps each character to its representative number.

The simplest scheme in common use is called ASCII. It covers the common Latin characters you are probably most accustomed to working with. For these characters,

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
54 returns the ASCII value for character
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
56:

>>>

>>> ord('a')
97
>>> ord('#')
35

ASCII is fine as far as it goes. But there are many different languages in use in the world and countless symbols and glyphs that appear in digital media. The full set of characters that potentially may need to be represented in computer code far surpasses the ordinary Latin letters, numbers, and symbols you usually see.

Unicode is an ambitious standard that attempts to provide a numeric code for every possible character, in every possible language, on every possible platform. Python 3 supports Unicode extensively, including allowing Unicode characters within strings.

For More Information: See Unicode & Character Encodings in Python: A Painless Guide and in the Python documentation.

As long as you stay in the domain of the common characters, there is little practical difference between ASCII and Unicode. But the

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
51 function will return numeric values for Unicode characters as well:

>>>

>>> ord('€')
8364
>>> ord('∑')
8721

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
58

Returns a character value for the given integer.

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
50 does the reverse of
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
51. Given a numeric value
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
38,
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
58 returns a string representing the character that corresponds to
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
38:

>>>

>>> chr(97)
'a'
>>> chr(35)
'#'

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
50 handles Unicode characters as well:

>>>

>>> chr(8364)
'€'
>>> chr(8721)
'∑'

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
65

Returns the length of a string.

With

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
52, you can check Python string length.
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
65 returns the number of characters in
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37:

>>>

>>> s = 'I am a string.'
>>> len(s)
14

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
69

Returns a string representation of an object.

Virtually any object in Python can be rendered as a string.

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
69 returns the string representation of object
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
71:

>>>

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
0

Remove ads

String Indexing

Often in programming languages, individual items in an ordered set of data can be accessed directly using a numeric index or key value. This process is referred to as indexing.

In Python, strings are ordered sequences of character data, and thus can be indexed in this way. Individual characters in a string can be accessed by specifying the string name followed by a number in square brackets (

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
72).

String indexing in Python is zero-based: the first character in the string has index

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
73, the next has index
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
74, and so on. The index of the last character will be the length of the string minus one.

For example, a schematic diagram of the indices of the string

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
75 would look like this:

Cara menggunakan string object in python
String Indices

The individual characters can be accessed by index as follows:

>>>

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
1

Attempting to index beyond the end of the string results in an error:

>>>

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
2

String indices can also be specified with negative numbers, in which case indexing occurs from the end of the string backward:

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
76 refers to the last character,
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
77 the second-to-last character, and so on. Here is the same diagram showing both the positive and negative indices into the string
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
75:

Cara menggunakan string object in python
Positive and Negative String Indices

Here are some examples of negative indexing:

>>>

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
3

Attempting to index with negative numbers beyond the start of the string results in an error:

>>>

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
4

For any non-empty string

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37,
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
80 and
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
81 both return the last character. There isn’t any index that makes sense for an empty string.

String Slicing

Python also allows a form of indexing syntax that extracts substrings from a string, known as string slicing. If

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 is a string, an expression of the form
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
83 returns the portion of
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 starting with position
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
85, and up to but not including position
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
38:

>>>

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
5

Remember: String indices are zero-based. The first character in a string has index

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
73. This applies to both standard indexing and slicing.

Again, the second index specifies the first character that is not included in the result—the character

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
88 (
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
89) in the example above. That may seem slightly unintuitive, but it produces this result which makes sense: the expression
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
83 will return a substring that is
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
91 characters in length, in this case,
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
92.

If you omit the first index, the slice starts at the beginning of the string. Thus,

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
93 and
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
94 are equivalent:

>>>

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
6

Similarly, if you omit the second index as in

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
95, the slice extends from the first index through the end of the string. This is a nice, concise alternative to the more cumbersome
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
96:

>>>

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
7

For any string

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 and any integer
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
38 (
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
99),
>>> 'foo' * -8
''
00 will be equal to
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37:

>>>

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
8

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 = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
9

If the first index in a slice is greater than or equal to the second index, Python returns an empty string. This is yet another obfuscated way to generate an empty string, in case you were looking for one:

>>>

>>> 'foo' * -8
''
0

Negative indices can be used with slicing as well.

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
76 refers to the last character,
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
77 the second-to-last, and so on, just as with simple indexing. The diagram below shows how to slice the substring
>>> 'foo' * -8
''
04 from the string
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
75 using both positive and negative indices:

Cara menggunakan string object in python
String Slicing with Positive and Negative Indices

Here is the corresponding Python code:

>>>

>>> 'foo' * -8
''
1

Remove ads

Specifying a Stride in a String Slice

There is one more variant of the slicing syntax to discuss. Adding an additional

>>> 'foo' * -8
''
06 and a third index designates a stride (also called a step), which indicates how many characters to jump after retrieving each character in the slice.

For example, for the string

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
75, the slice
>>> 'foo' * -8
''
08 starts with the first character and ends with the last character (the whole string), and every second character is skipped. This is shown in the following diagram:

Cara menggunakan string object in python
String Indexing with Stride

Similarly,

>>> 'foo' * -8
''
09 specifies a slice starting with the second character (index
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
74) and ending with the last character, and again the stride value
>>> 'foo' * -8
''
11 causes every other character to be skipped:

Cara menggunakan string object in python
Another String Indexing with Stride

The illustrative REPL code is shown here:

>>>

>>> 'foo' * -8
''
2

As with any slicing, the first and second indices can be omitted, and default to the first and last characters respectively:

>>>

>>> 'foo' * -8
''
3

You can specify a negative stride value as well, in which case Python steps backward through the string. In that case, the starting/first index should be greater than the ending/second index:

>>>

>>> 'foo' * -8
''
4

In the above example,

>>> 'foo' * -8
''
12 means “start at the last character and step backward by
>>> 'foo' * -8
''
11, up to but not including the first character.”

When you are stepping backward, if the first and second indices are omitted, the defaults are reversed in an intuitive way: the first index defaults to the end of the string, and the second index defaults to the beginning. Here is an example:

>>>

>>> 'foo' * -8
''
5

This is a common paradigm for reversing a string:

>>>

>>> 'foo' * -8
''
6

Interpolating Variables Into a String

In Python version 3.6, 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 capability provided by f-strings is extensive and won’t be covered in full detail here. If you want to learn more, you can check out the Real Python article Python 3’s f-Strings: An Improved String Formatting Syntax (Guide). There is also a tutorial on Formatted Output coming up 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 directly within an f-string literal, and Python will replace the name with the corresponding value.

For example, suppose you want to display the result of an arithmetic calculation. You can do this with a straightforward

>>> 'foo' * -8
''
14 statement, separating numeric values and string literals by commas:

>>>

>>> 'foo' * -8
''
7

But this is cumbersome. To accomplish the same thing using an f-string:

  • Specify either a lowercase
    >>> 'foo' * -8
    ''
    
    15 or uppercase
    >>> 'foo' * -8
    ''
    
    16 directly before the opening quote of the string literal. This tells Python it is an f-string instead of a standard string.
  • Specify any variables to be interpolated in curly braces (
    >>> 'foo' * -8
    ''
    
    17).

Recast using an f-string, the above example looks much cleaner:

>>>

>>> 'foo' * -8
''
8

Any of Python’s three quoting mechanisms can be used to define an f-string:

>>>

>>> 'foo' * -8
''
9

Remove ads

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 data types you have seen so far are immutable. (Python does provide data types that are mutable, as you will soon see.)

A statement like this will cause an error:

>>>

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
0

In truth, there really isn’t much need to modify strings. You can usually easily accomplish what you want by generating a copy of the original string that has the desired change in place. There are very many ways to do this in Python. Here is one possibility:

>>>

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
1

There is also a built-in string method to accomplish this:

>>>

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
2

Read on for more information about built-in string methods!

Built-in String Methods

You learned in the tutorial on Variables in Python that Python is a highly object-oriented language. Every item of data in a Python program is an object.

You are also 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 procedure that is tightly associated with an object. Like a function, a method is called to perform a distinct task, but it is invoked on a specific object and has knowledge of its target object during execution.

The syntax for invoking a method on an object is as follows:

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
3

This invokes method

>>> 'foo' * -8
''
18 on object
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
71.
>>> 'foo' * -8
''
20 specifies the arguments passed to the method (if any).

You will explore much more about 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 built-in methods Python supports for operating on string objects.

In the following method definitions, arguments specified in square brackets (

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
72) are optional.

Case Conversion

Methods in this group perform case conversion on the target string.

>>> 'foo' * -8
''
22

Capitalizes the target string.

>>> 'foo' * -8
''
22 returns a copy of
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 with the first character converted to uppercase and all other characters converted to lowercase:

>>>

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
4

Non-alphabetic characters are unchanged:

>>>

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
5

>>> 'foo' * -8
''
25

Converts alphabetic characters to lowercase.

>>> 'foo' * -8
''
25 returns a copy of
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 with all alphabetic characters converted to lowercase:

>>>

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
6

>>> 'foo' * -8
''
28

Swaps case of alphabetic characters.

>>> 'foo' * -8
''
28 returns a copy of
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 with uppercase alphabetic characters converted to lowercase and vice versa:

>>>

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
7

>>> 'foo' * -8
''
31

Converts the target string to “title case.”

>>> 'foo' * -8
''
31 returns a copy of
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 in which the first letter of each word is converted to uppercase and remaining letters are lowercase:

>>>

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
8

This method uses a fairly simple algorithm. It does not attempt to distinguish between important and unimportant words, and it does not handle apostrophes, possessives, or acronyms gracefully:

>>>

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
9

>>> 'foo' * -8
''
34

Converts alphabetic characters to uppercase.

>>> 'foo' * -8
''
34 returns a copy of
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 with all alphabetic characters converted to uppercase:

>>>

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
0

Find and Replace

These methods provide various means of searching the target string for a specified substring.

Each method in this group supports optional

>>> 'foo' * -8
''
37 and
>>> 'foo' * -8
''
38 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
>>> 'foo' * -8
''
37 and proceeding up to but not including character position
>>> 'foo' * -8
''
38. If
>>> 'foo' * -8
''
37 is specified but
>>> 'foo' * -8
''
38 is not, the method applies to the portion of the target string from
>>> 'foo' * -8
''
37 through the end of the string.

>>> 'foo' * -8
''
44

Counts occurrences of a substring in the target string.

>>> 'foo' * -8
''
45 returns the number of non-overlapping occurrences of substring
>>> 'foo' * -8
''
46 in
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37:

>>>

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
1

The count is restricted to the number of occurrences within the substring indicated by

>>> 'foo' * -8
''
37 and
>>> 'foo' * -8
''
38, if they are specified:

>>>

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
2

>>> 'foo' * -8
''
50

Determines whether the target string ends with a given substring.

>>> 'foo' * -8
''
51 returns
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
47 if
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 ends with the specified
>>> 'foo' * -8
''
54 and
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
48 otherwise:

>>>

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
3

The comparison is restricted to the substring indicated by

>>> 'foo' * -8
''
37 and
>>> 'foo' * -8
''
38, if they are specified:

>>>

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
4

>>> 'foo' * -8
''
58

Searches the target string for a given substring.

You can use

>>> 'foo' * -8
''
59 to see if a Python string contains a particular substring.
>>> 'foo' * -8
''
60 returns the lowest index in
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 where substring
>>> 'foo' * -8
''
46 is found:

>>>

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
5

This method returns

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
76 if the specified substring is not found:

>>>

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
6

The search is restricted to the substring indicated by

>>> 'foo' * -8
''
37 and
>>> 'foo' * -8
''
38, if they are specified:

>>>

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
7

>>> 'foo' * -8
''
66

Searches the target string for a given substring.

This method is identical to

>>> 'foo' * -8
''
59, except that it raises an exception if
>>> 'foo' * -8
''
46 is not found rather than returning
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
76:

>>>

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
8

>>> 'foo' * -8
''
70

Searches the target string for a given substring starting at the end.

>>> 'foo' * -8
''
71 returns the highest index in
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 where substring
>>> 'foo' * -8
''
46 is found:

>>>

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
9

As with

>>> 'foo' * -8
''
59, if the substring is not found,
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
76 is returned:

>>>

>>> ord('a')
97
>>> ord('#')
35
0

The search is restricted to the substring indicated by

>>> 'foo' * -8
''
37 and
>>> 'foo' * -8
''
38, if they are specified:

>>>

>>> ord('a')
97
>>> ord('#')
35
1

>>> 'foo' * -8
''
78

Searches the target string for a given substring starting at the end.

This method is identical to

>>> 'foo' * -8
''
79, except that it raises an exception if
>>> 'foo' * -8
''
46 is not found rather than returning
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
76:

>>>

>>> ord('a')
97
>>> ord('#')
35
2

>>> 'foo' * -8
''
82

Determines whether the target string starts with a given substring.

When you use the Python

>>> 'foo' * -8
''
83 method,
>>> 'foo' * -8
''
84 returns
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
47 if
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 starts with the specified
>>> 'foo' * -8
''
54 and
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
48 otherwise:

>>>

>>> ord('a')
97
>>> ord('#')
35
3

The comparison is restricted to the substring indicated by

>>> 'foo' * -8
''
37 and
>>> 'foo' * -8
''
38, if they are specified:

>>>

>>> ord('a')
97
>>> ord('#')
35
4

Character Classification

Methods in this group classify a string based on the characters it contains.

>>> 'foo' * -8
''
91

Determines whether the target string consists of alphanumeric characters.

>>> 'foo' * -8
''
91 returns
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
47 if
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 is nonempty and all its characters are alphanumeric (either a letter or a number), and
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
48 otherwise:

>>>

>>> ord('a')
97
>>> ord('#')
35
5

>>> 'foo' * -8
''
96

Determines whether the target string consists of alphabetic characters.

>>> 'foo' * -8
''
96 returns
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
47 if
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 is nonempty and all its characters are alphabetic, and
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
48 otherwise:

>>>

>>> ord('a')
97
>>> ord('#')
35
6

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
01

Determines whether the target string consists of digit characters.

You can use the

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
02 Python method to check if your string is made of only digits.
>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
01 returns
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
47 if
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 is nonempty and all its characters are numeric digits, and
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
48 otherwise:

>>>

>>> ord('a')
97
>>> ord('#')
35
7

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
07

Determines whether the target string is a valid Python identifier.

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
07 returns
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
47 if
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 is a valid Python identifier according to the language definition, and
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
48 otherwise:

>>>

>>> ord('a')
97
>>> ord('#')
35
8

Note:

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
12 will return
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
47 for a string that matches a Python keyword even though that would not actually be a valid identifier:

>>>

>>> ord('a')
97
>>> ord('#')
35
9

You can test whether a string matches a Python keyword using a function called

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
14, which is contained in a module called
>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
15. One possible way to do this is shown below:

>>>

>>> ord('€')
8364
>>> ord('∑')
8721
0

If you really want to ensure that a string would serve as a valid Python identifier, you should check that

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
12 is
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
47 and that
>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
14 is
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
48.

See Python Modules and Packages—An Introduction to read more about Python modules.

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
20

Determines whether the target string’s alphabetic characters are lowercase.

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
20 returns
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
47 if
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 is nonempty and all the alphabetic characters it contains are lowercase, and
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
48 otherwise. Non-alphabetic characters are ignored:

>>>

>>> ord('€')
8364
>>> ord('∑')
8721
1

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
25

Determines whether the target string consists entirely of printable characters.

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
25 returns
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
47 if
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 is empty or all the alphabetic characters it contains are printable. It returns
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
48 if
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 contains at least one non-printable character. Non-alphabetic characters are ignored:

>>>

>>> ord('€')
8364
>>> ord('∑')
8721
2

Note: This is the only

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
31 method that returns
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
47 if
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 is an empty string. All the others return
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
48 for an empty string.

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
35

Determines whether the target string consists of whitespace characters.

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
35 returns
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
47 if
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 is nonempty and all characters are whitespace characters, and
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
48 otherwise.

The most commonly encountered whitespace characters are space

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
40, tab
>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
41, and newline
>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
42:

>>>

>>> ord('€')
8364
>>> ord('∑')
8721
3

However, there are a few other ASCII characters that qualify as whitespace, and if you account for Unicode characters, there are quite a few beyond that:

>>>

>>> ord('€')
8364
>>> ord('∑')
8721
4

(

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
43 and
>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
44 are the escape sequences for the ASCII Form Feed and Carriage Return characters;
>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
45 is the escape sequence for the Unicode Four-Per-Em Space.)

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
46

Determines whether the target string is title cased.

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
46 returns
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
47 if
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 is nonempty, the first alphabetic character of each word is uppercase, and all other alphabetic characters in each word are lowercase. It returns
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
48 otherwise:

>>>

>>> ord('€')
8364
>>> ord('∑')
8721
5

Note: Here is how the Python documentation describes

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
51, in case you find this more intuitive: “Uppercase characters may only follow uncased characters and lowercase characters only cased ones.”

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
52

Determines whether the target string’s alphabetic characters are uppercase.

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
52 returns
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
47 if
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 is nonempty and all the alphabetic characters it contains are uppercase, and
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
48 otherwise. Non-alphabetic characters are ignored:

>>>

>>> ord('€')
8364
>>> ord('∑')
8721
6

String Formatting

Methods in this group modify or enhance the format of a string.

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
57

Centers a string in a field.

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
58 returns a string consisting of
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 centered in a field of width
>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
60. By default, padding consists of the ASCII space character:

>>>

>>> ord('€')
8364
>>> ord('∑')
8721
7

If the optional

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
61 argument is specified, it is used as the padding character:

>>>

>>> ord('€')
8364
>>> ord('∑')
8721
8

If

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 is already at least as long as
>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
60, it is returned unchanged:

>>>

>>> ord('€')
8364
>>> ord('∑')
8721
9

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
64

Expands tabs in a string.

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
65 replaces each tab character (
>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
41) with spaces. By default, spaces are filled in assuming a tab stop at every eighth column:

>>>

>>> chr(97)
'a'
>>> chr(35)
'#'
0

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
67 is an optional keyword parameter specifying alternate tab stop columns:

>>>

>>> chr(97)
'a'
>>> chr(35)
'#'
1

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
68

Left-justifies a string in field.

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
69 returns a string consisting of
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 left-justified in a field of width
>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
60. By default, padding consists of the ASCII space character:

>>>

>>> chr(97)
'a'
>>> chr(35)
'#'
2

If the optional

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
61 argument is specified, it is used as the padding character:

>>>

>>> chr(97)
'a'
>>> chr(35)
'#'
3

If

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 is already at least as long as
>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
60, it is returned unchanged:

>>>

>>> chr(97)
'a'
>>> chr(35)
'#'
4

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
75

Trims leading characters from a string.

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
76 returns a copy of
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 with any whitespace characters removed from the left end:

>>>

>>> chr(97)
'a'
>>> chr(35)
'#'
5

If the optional

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
78 argument is specified, it is a string that specifies the set of characters to be removed:

>>>

>>> chr(97)
'a'
>>> chr(35)
'#'
6

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
79

Replaces occurrences of a substring within a string.

In Python, to remove a character from a string, you can use the Python string

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
80 method.
>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
81 returns a copy of
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 with all occurrences of substring
>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
83 replaced by
>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
84:

>>>

>>> chr(97)
'a'
>>> chr(35)
'#'
7

If the optional

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
85 argument is specified, a maximum of
>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
85 replacements are performed, starting at the left end of
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37:

>>>

>>> chr(97)
'a'
>>> chr(35)
'#'
8

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
88

Right-justifies a string in a field.

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
89 returns a string consisting of
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 right-justified in a field of width
>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
60. By default, padding consists of the ASCII space character:

>>>

>>> chr(97)
'a'
>>> chr(35)
'#'
9

If the optional

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
61 argument is specified, it is used as the padding character:

>>>

>>> chr(8364)
'€'
>>> chr(8721)
'∑'
0

If

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 is already at least as long as
>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
60, it is returned unchanged:

>>>

>>> chr(8364)
'€'
>>> chr(8721)
'∑'
1

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
95

Trims trailing characters from a string.

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
96 returns a copy of
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 with any whitespace characters removed from the right end:

>>>

>>> chr(8364)
'€'
>>> chr(8721)
'∑'
2

If the optional

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
78 argument is specified, it is a string that specifies the set of characters to be removed:

>>>

>>> chr(8364)
'€'
>>> chr(8721)
'∑'
3

>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
99

Strips characters from the left and right ends of a string.

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
00 is essentially equivalent to invoking
>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
76 and
>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
96 in succession. Without the
>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
78 argument, it removes leading and trailing whitespace:

>>>

>>> chr(8364)
'€'
>>> chr(8721)
'∑'
4

As with

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
04 and
>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
05, the optional
>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
78 argument specifies the set of characters to be removed:

>>>

>>> chr(8364)
'€'
>>> chr(8721)
'∑'
5

Note: When the return value of a string method is another string, as is often the case, methods can be invoked in succession by chaining the calls:

>>>

>>> chr(8364)
'€'
>>> chr(8721)
'∑'
6

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
07

Pads a string on the left with zeros.

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
07 returns a copy of
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 left-padded with
>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
10 characters to the specified
>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
60:

>>>

>>> chr(8364)
'€'
>>> chr(8721)
'∑'
7

If

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 contains a leading sign, it remains at the left edge of the result string after zeros are inserted:

>>>

>>> chr(8364)
'€'
>>> chr(8721)
'∑'
8

If

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 is already at least as long as
>>> s = 'foo'

>>> s in 'That\'s food for thought.'
True
>>> s in 'That\'s good for now.'
False
60, it is returned unchanged:

>>>

>>> chr(8364)
'€'
>>> chr(8721)
'∑'
9

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
15 is most useful for string representations of numbers, but Python will still happily zero-pad a string that isn’t:

>>>

>>> s = 'I am a string.'
>>> len(s)
14
0

Converting Between Strings and Lists

Methods in this group convert between a string and some composite data type by either pasting objects together to make a string, or by breaking a string up into pieces.

These methods operate on or return iterables, the general Python term for a sequential collection of objects. You will explore the inner workings of iterables in much more detail in the upcoming tutorial on definite iteration.

Many of these methods return either a list or a tuple. These are two similar composite data types that are prototypical examples of iterables in Python. They are covered in the next tutorial, so you’re about to learn about them soon! Until then, simply think of them as sequences of values. A list is enclosed in square brackets (

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
72), and a tuple is enclosed in parentheses (
>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
17).

With that introduction, let’s take a look at this last group of string methods.

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
18

Concatenates strings from an iterable.

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
18 returns the string that results from concatenating the objects in
>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
20 separated by
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37.

Note that

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
22 is invoked on
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37, the separator string.
>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
20 must be a sequence of string objects as well.

Some sample code should help clarify. In the following example, the separator

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 is the string
>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
26, and
>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
20 is a list of string values:

>>>

>>> s = 'I am a string.'
>>> len(s)
14
1

The result is a single string consisting of the list objects separated by commas.

In the next example,

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
20 is specified as a single string value. When a string value is used as an iterable, it is interpreted as a list of the string’s individual characters:

>>>

>>> s = 'I am a string.'
>>> len(s)
14
2

Thus, the result of

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
29 is a string consisting of each character in
>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
30 separated by
>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
31.

This example fails because one of the objects in

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
20 is not a string:

>>>

>>> s = 'I am a string.'
>>> len(s)
14
3

That can be remedied, though:

>>>

>>> s = 'I am a string.'
>>> len(s)
14
4

As you will soon see, many composite objects in Python can be construed as iterables, and

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
22 is especially useful for creating strings from them.

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
34

Divides a string based on a separator.

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
34 splits
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 at the first occurrence of string
>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
37. The return value is a three-part tuple consisting of:

  • The portion of
    >>> s = 'foo.'
    
    >>> s * 4
    'foo.foo.foo.foo.'
    >>> 4 * s
    'foo.foo.foo.foo.'
    
    37 preceding
    >>> 'z' not in 'abc'
    True
    >>> 'z' not in 'xyz'
    False
    
    37
  • >>> 'z' not in 'abc'
    True
    >>> 'z' not in 'xyz'
    False
    
    37 itself
  • The portion of
    >>> s = 'foo.'
    
    >>> s * 4
    'foo.foo.foo.foo.'
    >>> 4 * s
    'foo.foo.foo.foo.'
    
    37 following
    >>> 'z' not in 'abc'
    True
    >>> 'z' not in 'xyz'
    False
    
    37

Here are a couple examples of

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
43 in action:

>>>

>>> s = 'I am a string.'
>>> len(s)
14
5

If

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
37 is not found in
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37, the returned tuple contains
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 followed by two empty strings:

>>>

>>> s = 'I am a string.'
>>> len(s)
14
6

Remember: Lists and tuples are covered in the next tutorial.

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
47

Divides a string based on a separator.

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
47 functions exactly like
>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
34, except that
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 is split at the last occurrence of
>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
37 instead of the first occurrence:

>>>

>>> s = 'I am a string.'
>>> len(s)
14
7

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
52

Splits a string into a list of substrings.

Without arguments,

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
53 splits
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 into substrings delimited by any sequence of whitespace and returns the substrings as a list:

>>>

>>> s = 'I am a string.'
>>> len(s)
14
8

If

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
37 is specified, it is used as the delimiter for splitting:

>>>

>>> s = 'I am a string.'
>>> len(s)
14
9

(If

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
37 is specified with a value of
>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
57, the string is split delimited by whitespace, just as though
>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
37 had not been specified at all.)

When

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
37 is explicitly given as a delimiter, consecutive delimiters in
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 are assumed to delimit empty strings, which will be returned:

>>>

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
00

This is not the case when

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
37 is omitted, however. In that case, consecutive whitespace characters are combined into a single delimiter, and the resulting list will never contain empty strings:

>>>

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
01

If the optional keyword parameter

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
62 is specified, a maximum of that many splits are performed, starting from the right end of
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37:

>>>

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
02

The default value for

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
62 is
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
76, which means all possible splits should be performed—the same as if
>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
62 is omitted entirely:

>>>

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
03

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
67

Splits a string into a list of substrings.

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
68 behaves exactly like
>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
53, except that if
>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
62 is specified, splits are counted from the left end of
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 rather than the right end:

>>>

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
04

If

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
62 is not specified,
>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
73 and
>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
74 are indistinguishable.

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
75

Breaks a string at line boundaries.

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
76 splits
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
37 up into lines and returns them in a list. Any of the following characters or character sequences is considered to constitute a line boundary:

Escape SequenceCharacter

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
78Newline
>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
79Carriage Return
>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
80Carriage Return + Line Feed
>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
81 or
>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
82Line Tabulation
>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
83 or
>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
84Form Feed
>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
85File Separator
>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
86Group Separator
>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
87Record Separator
>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
88Next Line (C1 Control Code)
>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
89Unicode Line Separator
>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
90Unicode Paragraph Separator

Here is an example using several different line separators:

>>>

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
05

If consecutive line boundary characters are present in the string, they are assumed to delimit blank lines, which will appear in the result list:

>>>

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
06

If the optional

>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
91 argument is specified and is truthy, then the lines boundaries are retained in the result strings:

>>>

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
07

Remove ads

>>> s = 'foo.' >>> s * 4 'foo.foo.foo.foo.' >>> 4 * s 'foo.foo.foo.foo.' 29 Objects

The

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
29 object is one of the core built-in types for manipulating binary data. A
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
29 object is an immutable sequence of single byte values. Each element in a
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
29 object is a small integer in the range
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
73 to
>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
97.

Defining a Literal >>> s = 'foo.' >>> s * 4 'foo.foo.foo.foo.' >>> 4 * s 'foo.foo.foo.foo.' 29 Object

A

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
29 literal is defined in the same way as a string literal with the addition of a
>>> ord('a')
97
>>> ord('#')
35
00 prefix:

>>>

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
08

As with strings, you can use any of the single, double, or triple quoting mechanisms:

>>>

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
09

Only ASCII characters are allowed in a

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
29 literal. Any character value greater than
>>> ord('a')
97
>>> ord('#')
35
02 must be specified using an appropriate escape sequence:

>>>

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
10

The

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
88 prefix may be used on a
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
29 literal to disable processing of escape sequences, as with strings:

>>>

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
11

Defining a >>> s = 'foo.' >>> s * 4 'foo.foo.foo.foo.' >>> 4 * s 'foo.foo.foo.foo.' 29 Object With the Built-in >>> ord('a') 97 >>> ord('#') 35 06 Function

The

>>> ord('a')
97
>>> ord('#')
35
06 function also creates a
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
29 object. What sort of
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
29 object gets returned depends on the argument(s) passed to the function. The possible forms are shown below.

>>> ord('a')
97
>>> ord('#')
35
10

Creates a

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
29 object from a string.

>>> ord('a')
97
>>> ord('#')
35
10 converts string
>>> ord('a')
97
>>> ord('#')
35
13 to a
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
29 object, using
>>> ord('a')
97
>>> ord('#')
35
15 according to the specified
>>> ord('a')
97
>>> ord('#')
35
16:

>>>

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
12

Technical Note: In this form of the

>>> ord('a')
97
>>> ord('#')
35
06 function, the
>>> ord('a')
97
>>> ord('#')
35
16 argument is required. “Encoding” refers to the manner in which characters are translated to integer values. A value of
>>> ord('a')
97
>>> ord('#')
35
19 indicates Unicode Transformation Format UTF-8, which is an encoding that can handle every possible Unicode character. UTF-8 can also be indicated by specifying
>>> ord('a')
97
>>> ord('#')
35
20,
>>> ord('a')
97
>>> ord('#')
35
21, or
>>> ord('a')
97
>>> ord('#')
35
22 for
>>> ord('a')
97
>>> ord('#')
35
16.

See the Unicode documentation for more information. As long as you are dealing with common Latin-based characters, UTF-8 will serve you fine.

>>> ord('a')
97
>>> ord('#')
35
24

Creates a

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
29 object consisting of null (
>>> ord('a')
97
>>> ord('#')
35
26) bytes.

>>> ord('a')
97
>>> ord('#')
35
24 defines a
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
29 object of the specified
>>> ord('a')
97
>>> ord('#')
35
29, which must be a positive integer. The resulting
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
29 object is initialized to null (
>>> ord('a')
97
>>> ord('#')
35
26) bytes:

>>>

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
13

>>> ord('a')
97
>>> ord('#')
35
32

Creates a

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
29 object from an iterable.

>>> ord('a')
97
>>> ord('#')
35
32 defines a
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
29 object from the sequence of integers generated by
>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
20.
>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
20 must be an iterable that generates a sequence of integers
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
38 in the range
>>> ord('a')
97
>>> ord('#')
35
39:

>>>

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
14

Remove ads

Operations on >>> s = 'foo.' >>> s * 4 'foo.foo.foo.foo.' >>> 4 * s 'foo.foo.foo.foo.' 29 Objects

Like strings,

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
29 objects support the common sequence operations:

  • The

    >>> s = 'foo.'
    
    >>> s * 4
    'foo.foo.foo.foo.'
    >>> 4 * s
    'foo.foo.foo.foo.'
    
    45 and
    >>> s = 'foo.'
    
    >>> s * 4
    'foo.foo.foo.foo.'
    >>> 4 * s
    'foo.foo.foo.foo.'
    
    49 operators:

    >>>

    >>> s = 'foo.'
    
    >>> s * 4
    'foo.foo.foo.foo.'
    >>> 4 * s
    'foo.foo.foo.foo.'
    
    15

  • The concatenation (

    >>> s = 'foo.'
    
    >>> s * 4
    'foo.foo.foo.foo.'
    >>> 4 * s
    'foo.foo.foo.foo.'
    
    31) and replication (
    >>> s = 'foo.'
    
    >>> s * 4
    'foo.foo.foo.foo.'
    >>> 4 * s
    'foo.foo.foo.foo.'
    
    32) operators:

    >>>

    >>> s = 'foo.'
    
    >>> s * 4
    'foo.foo.foo.foo.'
    >>> 4 * s
    'foo.foo.foo.foo.'
    
    16

  • Indexing and slicing:

    >>>

    >>> s = 'foo.'
    
    >>> s * 4
    'foo.foo.foo.foo.'
    >>> 4 * s
    'foo.foo.foo.foo.'
    
    17

  • Built-in functions:

    >>>

    >>> s = 'foo.'
    
    >>> s * 4
    'foo.foo.foo.foo.'
    >>> 4 * s
    'foo.foo.foo.foo.'
    
    18

Many of the methods defined for string objects are valid for

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
29 objects as well:

>>>

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
19

Notice, however, that when these operators and methods are invoked on a

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
29 object, the operand and arguments must be
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
29 objects as well:

>>>

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
20

Although a

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
29 object definition and representation is based on ASCII text, it actually behaves like an immutable sequence of small integers in the range
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
73 to
>>> 'z' not in 'abc'
True
>>> 'z' not in 'xyz'
False
97, inclusive. That is why a single element from a
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
29 object is displayed as an integer:

>>>

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
21

A slice is displayed as a

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
29 object though, even if it is only one byte long:

>>>

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
22

You can convert a

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
29 object into a list of integers with the built-in
>>> ord('a')
97
>>> ord('#')
35
55 function:

>>>

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
23

Hexadecimal numbers are often used to specify binary data because two hexadecimal digits correspond directly to a single byte. The

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
29 class supports two additional methods that facilitate conversion to and from a string of hexadecimal digits.

>>> ord('a')
97
>>> ord('#')
35
57

Returns a

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
29 object constructed from a string of hexadecimal values.

>>> ord('a')
97
>>> ord('#')
35
57 returns the
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
29 object that results from converting each pair of hexadecimal digits in
>>> ord('a')
97
>>> ord('#')
35
13 to the corresponding byte value. The hexadecimal digit pairs in
>>> ord('a')
97
>>> ord('#')
35
13 may optionally be separated by whitespace, which is ignored:

>>>

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
24

Note: This method is a class method, not an object method. It is bound to the

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
29 class, not a
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
29 object. You will delve much more into the distinction between classes, objects, and their respective methods in the upcoming tutorials on object-oriented programming. For now, just observe that this method is invoked on the
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
29 class, not on object
>>> ord('a')
97
>>> ord('#')
35
66.

>>> ord('a')
97
>>> ord('#')
35
67

Returns a string of hexadecimal value from a

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
29 object.

>>> ord('a')
97
>>> ord('#')
35
67 returns the result of converting
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
29 object
>>> ord('a')
97
>>> ord('#')
35
66 into a string of hexadecimal digit pairs. That is, it does the reverse of
>>> ord('a')
97
>>> ord('#')
35
72:

>>>

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
25

Note: As opposed to

>>> ord('a')
97
>>> ord('#')
35
72,
>>> ord('a')
97
>>> ord('#')
35
74 is an object method, not a class method. Thus, it is invoked on an object of the
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
29 class, not on the class itself.

Remove ads

>>> s = 'foo.' >>> s * 4 'foo.foo.foo.foo.' >>> 4 * s 'foo.foo.foo.foo.' 30 Objects

Python supports another binary sequence type called the

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
30.
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
30 objects are very like
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
29 objects, despite some differences:

  • There is no dedicated syntax built into Python for defining a

    >>> s = 'foo.'
    
    >>> s * 4
    'foo.foo.foo.foo.'
    >>> 4 * s
    'foo.foo.foo.foo.'
    
    30 literal, like the
    >>> ord('a')
    97
    >>> ord('#')
    35
    
    00 prefix that may be used to define a
    >>> s = 'foo.'
    
    >>> s * 4
    'foo.foo.foo.foo.'
    >>> 4 * s
    'foo.foo.foo.foo.'
    
    29 object. A
    >>> s = 'foo.'
    
    >>> s * 4
    'foo.foo.foo.foo.'
    >>> 4 * s
    'foo.foo.foo.foo.'
    
    30 object is always created using the
    >>> ord('a')
    97
    >>> ord('#')
    35
    
    84 built-in function:

    >>>

    >>> s = 'foo.'
    
    >>> s * 4
    'foo.foo.foo.foo.'
    >>> 4 * s
    'foo.foo.foo.foo.'
    
    26

  • >>> s = 'foo.'
    
    >>> s * 4
    'foo.foo.foo.foo.'
    >>> 4 * s
    'foo.foo.foo.foo.'
    
    30 objects are mutable. You can modify the contents of a
    >>> s = 'foo.'
    
    >>> s * 4
    'foo.foo.foo.foo.'
    >>> 4 * s
    'foo.foo.foo.foo.'
    
    30 object using indexing and slicing:

    >>>

    >>> s = 'foo.'
    
    >>> s * 4
    'foo.foo.foo.foo.'
    >>> 4 * s
    'foo.foo.foo.foo.'
    
    27

A

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
30 object may be constructed directly from a
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
29 object as well:

>>>

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
28

Conclusion

This tutorial provided an in-depth look at the many different mechanisms Python provides for string handling, including string operators, built-in functions, indexing, slicing, and built-in methods. You also were introduced to the

>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
29 and
>>> s = 'foo.'

>>> s * 4
'foo.foo.foo.foo.'
>>> 4 * s
'foo.foo.foo.foo.'
30 types.

These types are the first types you have examined that are composite—built from a collection of smaller parts. Python provides several composite built-in types. In the next tutorial, you will explore two of the most frequently used: lists and tuples.

Take the Quiz: Test your knowledge with our interactive “Python Strings and Character Data” quiz. Upon completion you will receive a score so you can track your learning progress over time:

Take the Quiz »

« Operators and Expressions in Python

Lists and Tuples in Python »

Mark as Completed

Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Strings and Character Data in Python

🐍 Python Tricks 💌

Get a short & sweet Python Trick delivered to your inbox every couple of days. No spam ever. Unsubscribe any time. Curated by the Real Python team.

Cara menggunakan string object in python

Send Me Python Tricks »

About John Sturtz

Cara menggunakan string object in python
Cara menggunakan string object in python

John is an avid Pythonista and a member of the Real Python tutorial team.

» More about John


Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:

Cara menggunakan string object in python

Dan

Cara menggunakan string object in python

Joanna

Master Real-World Python Skills With Unlimited Access to Real Python

Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:

Level Up Your Python Skills »

Master Real-World Python Skills
With Unlimited Access to Real Python

Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:

Level Up Your Python Skills »

What Do You Think?

Rate this article:

Tweet Share Share Email

What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment below and let us know.

Commenting Tips: The most useful comments are those written with the goal of learning from or helping out other students. and get answers to common questions in our support portal.

Apa itu object dalam python?

Objek pada python adalah kumpulan dari variabel-variabel (dinamakan atribut) dan kumpulan dari fungsi-fungsi (dinamakan perilaku). Atas definisi itu, maka semua hal di dalam python adalah sebuah Objek. Objek dan Kelas dalam python bermakna sama.

Apakah string dapat digunakan pada python?

String dalam python adalah bytes array yang mempresentasikan unicode char. Python tidak punya tipe data char, sehingga char pada python diganti dengan string yang punya panjang satu karakter. Dalam mengakses elemen pada string menggunakan brackets atau kurung siku [].

F string untuk apa?

Formatted string literals (juga disebut f-string) memungkinkan Anda menyertakan nilai ekspresi Python di dalam string dengan mengawali string dengan f atau F dan menulis ekspresi sebagai {expression} .

Apa fungsi split () python?

Sesuai dengan namanya, split artinya membagi atau memisahkan. Dapat disimpulkan bahwa metode split Python merupakan cara untuk membagi data string menjadi daftar string setelah memecah string yang diberikan oleh pemisah yang sudah ditentukan.