[Introduction to Udemy Python3 + Application] 12. Indexing and slicing of character strings

** * This article is from Udemy "[Introduction to Python3 taught by active Silicon Valley engineers + application + American Silicon Valley style code style](https://www.udemy.com/course/python-beginner/" Introduction to Python3 taught by active Silicon Valley engineers + application + American Silicon Valley style code style ")" It is a class notebook for myself after taking the course of. It is open to the public with permission from the instructor Jun Sakai. ** **

■ Index

◆ How to use the index

index


word = 'python'

print(word[0])
print(word[1])
print(word[2])
print(word[3])
print(word[4])
print(word[5])

result


p
y
t
h
o
n

When you assign a string to a variable, you can specify any character in that string. Note that in Python, the index for ** "first character" is "0" **.

◆ Index error

index


word = 'python'

print(word[100])

result


    print(word[100])
IndexError: string index out of range

At this time, if an index exceeding the range of the character string is specified, an error will occur.

◆ Index "-"

index


word = 'python'

print(word[-1])
print(word[-2])
print(word[-3])

result


n
o
h

[-1] etc. are counted so that they go further to the left from the first character ([0]) and turn to the last character. Therefore, [-1] is an index representing the last character.


■ Slice

◆ How to use slices

slice


word = 'python'

print(word[0:2])

result


py

With [:], you can specify the start point and end point, and specify the number between them.

|p|y|t|h|o|n|
0 1 2 3 4 5 6

The index actually represents between letters, and it is easy to understand if you think in this way. (Between 0 and 2 is "py".)

◆ Omitted

slice


word = 'python'

print(word[:2])
print(word[2:])
print(word[:])

result


py
thon
python

If you omit the start point, it will be "from the beginning", If the end point is omitted, it will be "until the end". If both the start point and the end point are omitted, the entire character string will be specified.


■ Replace only any character in the character string

◆「python」→「jython」

change_letter


word = 'python'
word = 'j' + word[1:]
print(word)

result


jython

■ Count the number of characters

count_letters


word = 'python'

n = len(word)
print(n)

result


6

The "length" of a string can be counted using len ().

Recommended Posts

[Introduction to Udemy Python3 + Application] 12. Indexing and slicing of character strings
[Introduction to Udemy Python3 + Application] 11. Character strings
[Introduction to Udemy Python3 + Application] 13. Character method
[Introduction to Udemy Python 3 + Application] 26. Copy of dictionary
[Introduction to Udemy Python 3 + Application] 19. Copy of list
[Introduction to Udemy Python3 + Application] 64. Namespace and Scope
[Introduction to Udemy Python3 + Application] 14. Character substitution 15.f-strings
[Introduction to Udemy Python3 + Application] 35. Comparison operators and logical operators
[Introduction to Udemy Python3 + Application] 68. Import statement and AS
[Introduction to Udemy Python3 + Application] 52. Tupleization of positional arguments
[Introduction to Udemy Python 3 + Application] 58. Lambda
[Introduction to Udemy Python 3 + Application] 57. Decorator
[Introduction to Udemy Python 3 + Application] 56. Closure
[Introduction to Udemy Python3 + Application] 59. Generator
[Introduction to Udemy Python 3 + Application] Summary
[Introduction to Udemy Python3 + Application] 42. for statement, break statement, and continue statement
[Introduction to Udemy Python3 + Application] 39. while statement, continue statement and break statement
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
[Introduction to Udemy Python3 + Application] 50. Positional arguments, keyword arguments, and default arguments
[Introduction to Udemy Python3 + Application] 18. List methods
[Introduction to Udemy Python3 + Application] 63. Generator comprehension
[Introduction to Udemy Python3 + Application] 28. Collective type
[Introduction to Udemy Python3 + Application] 25. Dictionary-type method
[Introduction to Udemy Python3 + Application] 33. if statement
[Introduction to Udemy Python3 + Application] 55. In-function functions
[Introduction to Udemy Python3 + Application] 48. Function definition
[Introduction to Udemy Python 3 + Application] 10. Numerical values
[Introduction to Udemy Python3 + Application] 21. Tuple type
[Introduction to Udemy Python3 + Application] 45. enumerate function
[Introduction to Udemy Python3 + Application] 65. Exception handling
[Introduction to Udemy Python3 + Application] 44. range function
[Introduction to Udemy Python3 + Application] 46. Zip function
[Introduction to Udemy Python3 + Application] 24. Dictionary type
[Introduction to Udemy Python3 + Application] 8. Variable declaration
[Introduction to Udemy Python3 + Application] 29. Set method
[Introduction to Udemy Python3 + Application] 16. List type
[Introduction to Udemy Python3 + Application] 61. Dictionary comprehension
[Introduction to Udemy Python 3 + Application] 22. Tuple unpacking
[Introduction to Udemy Python3 + Application] 23. How to use tuples
[Introduction to Udemy Python3 + Application] 60. List comprehension notation
[Introduction to Udemy Python 3 + Application] 38. When judging None
[Introduction to Udemy Python3 + Application] 40.while else statement
[Introduction to Udemy Python3 + Application] 43. for else statement
[Introduction to Udemy Python3 + Application] 67. Command line arguments
[Introduction to Udemy Python3 + Application] 9. First, print with print
[Introduction to Udemy Python 3 + Application] 54. What is Docstrings?
3-3, Python strings and character codes
[Introduction to Udemy Python 3 + Application] 66. Creating your own exceptions
[Introduction to Udemy Python3 + Application] 27. How to use the dictionary
[Introduction to Udemy Python3 + Application] 30. How to use the set
[Introduction to Data Scientists] Basics of Python ♬ Functions and classes
[Introduction to Udemy Python3 + Application] 51. Be careful with default arguments
[Introduction to Python3 Day 1] Programming and Python
[Introduction to Python3 Day 13] Chapter 7 Strings (7.1-7.1.1.1)
[Introduction to Python3 Day 14] Chapter 7 Strings (7.1.1.1 to 7.1.1.4)
[Introduction to Python3 Day 15] Chapter 7 Strings (7.1.2-7.1.2.2)
[Introduction to Python] I compared the naming conventions of C # and Python.
[Introduction to Data Scientists] Basics of Python ♬ Conditional branching and loops
[Introduction to Data Scientists] Basics of Python ♬ Functions and anonymous functions, etc.
Easy introduction of python3 series and OpenCV3
[Python] Various combinations of strings and values