markdown 2. Python - 字符串操作
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 2. Python - 字符串操作相关的知识,希望对你有一定的参考价值。
## Table of Contents
<div class="alert alert-block alert-info" style="margin-top: 20px">
<li><a href="#ref0">Strings</a></li>
<li><a href="#ref1">Indexing</a></li>
<li><a href="#ref2">Escape Sequences </a></li>
<li><a href="#ref3">String Operations</a></li>
<li><a href="#ref4">Quizz </a></li>
<br>
<p></p>
Estimated Time Needed: <strong>15 min</strong>
</div>
<hr>
<a id="ref0"></a>
<h2 align=center>Strings </h2>
A string is contained within 2 quotes:
```python
"Michael Jackson"
```
'Michael Jackson'
You can also use single quotes:
```python
'Michael Jackson'
```
'Michael Jackson'
A string can be spaces and digits:
```python
'1 2 3 4 5 6 '
```
'1 2 3 4 5 6 '
A string can also be special characters :
```python
'@#2_#]&*^%$'
```
'@#2_#]&*^%$'
We can print our string using the print statement:
```python
print("hello!")
```
hello!
We can bind or assign a string to another variable:
```python
Name= "Michael Jackson"
Name
```
'Michael Jackson'
<a id="ref1"></a>
<h2 align=center>Indexing </h2>
It is helpful to think of a string as an ordered sequence. Each element in the sequence can be accessed using an index represented by the array of numbers:
<img src = "https://ibm.box.com/shared/static/esdu9w118rm1aldff7ff8c9b3kpjdazc.png" width = 600, align = "center"></a>
The first index can be accessed as follows:
```python
print(Name[13])
```
o
We can access index 6:
```python
print(Name[6])
```
l
Moreover, we can access the 13th index:
```python
print(Name[13])
```
o
We can also use negative indexing with strings:
<img src = "https://ibm.box.com/shared/static/rmq8akevtt6uufox3xai7q2kqs4j9kan.png" width = 600, align = "center"></a>
The last element is given by the index -1:
```python
print(Name[-1])
```
n
The first element can be obtained by index -15:
```python
print(Name[-15])
```
M
We can find the number of characters in a string by using 'len', short for length:
```python
len("Michael Jackson")
```
15
We can obtain multiple characters from a string using slicing, we can obtain the 0 to 4th and 8th to the 12th element:
<img src="https://ibm.box.com/shared/static/bva43bmp00cxeunqh4w7blkgniycbign.png" width=600,align="center"></a>
```python
Name[0:4]
```
'Mich'
```python
Name[8:12]
```
'Jack'
We can also input a stride value as follows, with the '2' indicating that we are selecting every second variable:
<img src="https://ibm.box.com/shared/static/f49xvym409rxclhtbr30xrs9kc4l5419.png" width=600,align="center"></a>
```python
Name[::2]
```
'McalJcsn'
We can also incorporate slicing with the stride. In this case, we select the first five elements and then use the stride:
```python
Name[0:5:2]
```
'Mca'
We can concatenate or combine strings by using the addition symbols, and the result is a new string that is a combination of both:
```python
Statement = Name + "is the best"
Statement
```
'Michael Jacksonis the best'
To replicate values of a string we simply multiply the string by the number of times we would like to replicate it. In this case, the number is three. The result is a new string, and this new string consists of three copies of the original string:
```python
3*"Michael Jackson "
```
'Michael Jackson Michael Jackson Michael Jackson '
You can create a new string by setting it to the original variable. Concatenated with a new string, the result is a new string that changes from Michael Jackson to “Michael Jackson is the best".
```python
Name= "Michael Jackson"
Name= Name+" is the best"
Name
```
'Michael Jackson is the best'
<a id="ref2"></a>
<h2 align=center>Escape Sequences </h2>
Back slashes represent the beginning of escape sequences. Escape sequences represent strings that may be difficult to input. For example, back slash "n" represents a new line. The output is given by a new line after the back slash "n” is encountered:
```python
print(" Michael Jackson \n is the best" )
```
Michael Jackson
is the best
Similarly, back slash "t" represents a tab:
```python
print(" Michael Jackson \t is the best" )
```
Michael Jackson is the best
If you want to place a back slash in your string, use a double back slash:
```python
print(" Michael Jackson \\ is the best" )
```
Michael Jackson \ is the best
We can also place an "r" before the string to display the backslash:
```python
print(r" Michael Jackson \ is the best" )
```
Michael Jackson \ is the best
<a id="ref3"></a>
<h2 align=center>String Operations</h2>
There are many string operation methods in Python that can be used to manipulate the data. We are going to use some basic string operations on the data.
Let's try with the method "upper"; this method converts lower case characters to upper case characters:
```python
A="Thriller is the sixth studio album"
print("before upper:",A)
B=A.upper()
print("After upper:",B)
```
before upper: Thriller is the sixth studio album
After upper: THRILLER IS THE SIXTH STUDIO ALBUM
<br>
The method replaces a segment of the string, i.e. a substring with a new string. We input the part of the string we would like to change. The second argument is what we would like to exchange the segment with, and the result is a new string with the segment changed:
```python
A="Michael Jackson is the best"
B=A.replace('Michael', 'Janet')
B
```
'Janet Jackson is the best'
The method "find" finds a sub-string. The argument is the substring you would like to find, and the output is the first index of the sequence. We can find the sub-string "jack" or "el".
<img src="https://ibm.box.com/shared/static/mc414goh1l8jfo9gb19yibuylk8zk7dh.png" width=600,align="center"></a>
```python
Name="Michael Jackson"
Name.find('el')
```
5
```python
Name.find('Jack')
```
8
If the sub-string is not in the string then the output is a negative one. For example, the string 'Jasdfasdasdf' is not a substring:
```python
Name.find('Jasdfasdasdf')
```
-1
<a id="ref4"></a>
<h2 align=center> Quiz on Strings </h2>
##### What is the value of the variable "A" after the following code is executed?
A="1"
```python
```
<div align="right">
<a href="#String1" class="btn btn-default" data-toggle="collapse">Click here for the solution</a>
</div>
<div id="String1" class="collapse">
```
"1"
```
</div>
#### What is the value of the variable "B" after the following code is executed?
`B="2"`
```python
```
<div align="right">
<a href="#String2" class="btn btn-default" data-toggle="collapse">Click here for the solution</a>
</div>
<div id="String2" class="collapse">
```
"2"
```
</div>
#### What is the value of the variable "C" after the following code is executed?
`C=A+B`
```python
```
<div align="right">
<a href="#String3" class="btn btn-default" data-toggle="collapse">Click here for the solution</a>
</div>
<div id="String3" class="collapse">
```
"12"
```
</div>
#### Consider the variable "D": use slicing to print out the first three elements:
```python
D="ABCDEFG"
```
```python
```
<div align="right">
<a href="#String4" class="btn btn-default" data-toggle="collapse">Click here for the solution</a>
</div>
<div id="String4" class="collapse">
```
"print(D[:3]) or print(D[0:3])"
```
</div>
#### Use a stride value of 2 to print out every second character of the string "E":
```python
E='clocrkr1e1c1t'
```
```python
```
<div align="right">
<a href="#String6" class="btn btn-default" data-toggle="collapse">Click here for the solution</a>
</div>
<div id="String6" class="collapse">
```
"print(E[::2])"
```
</div>
#### Print out a backslash:
```python
```
<div align="right">
<a href="#String7" class="btn btn-default" data-toggle="collapse">Click here for the solution</a>
</div>
<div id="String7" class="collapse">
```
print(" \\" )
or
print(r" \ " )
```
</div>
#### Convert the variable "F" to uppercase:
```python
F="You are wrong"
```
```python
```
<div align="right">
<a href="#String8" class="btn btn-default" data-toggle="collapse">Click here for the solution</a>
</div>
<div id="String8" class="collapse">
```
F.upper()
```
</div>
#### Consider the variable "G", and find the first index of the sub-string 'snow':
```python
G="Mary had a little lamb Little lamb, little lamb Mary had a little lamb \
Its fleece was white as snow And everywhere that Mary went Mary went, Mary went \
Everywhere that Mary went The lamb was sure to go"
```
```python
```
<div align="right">
<a href="#String9" class="btn btn-default" data-toggle="collapse">Click here for the solution</a>
</div>
<div id="String9" class="collapse">
```
G.find('snow')
```
</div>
#### In the variable "G", replace the sub-string Mary with "Bob":
```python
```
<div align="right">
<a href="#String10" class="btn btn-default" data-toggle="collapse">Click here for the solution</a>
</div>
<div id="String10" class="collapse">
```
G.replace("Mary","Bob")
```
</div>
<a href="http://cocl.us/bottemNotebooksPython101Coursera"><img src = "https://ibm.box.com/shared/static/irypdxea2q4th88zu1o1tsd06dya10go.png" width = 750, align = "center"></a>
# About the Authors:
[Joseph Santarcangelo]( https://www.linkedin.com/in/joseph-s-50398b136/) has a PhD in Electrical Engineering, his research focused on using machine learning, signal processing, and computer vision to determine how videos impact human cognition. Joseph has been working for IBM since he completed his PhD.
```python
```
以上是关于markdown 2. Python - 字符串操作的主要内容,如果未能解决你的问题,请参考以下文章
markdown python ElementTree的tostring方法返回字节而不是字符串