python开发之字符串string操作方法实例详解
本文实例讲述了python开发之字符串string操作方法。分享给大家供大家参考,具体如下:
在python中,对于字符串string的操作,我们有必要了解一下,这样在我们的以后的开发中会给我们带来很多方便
下面是我学习的笔记:
#python-string #python中的字符串用单引号''和双引号""标示 strA = 'this is a string' strB = "this is a message!" #打印两个字符串 print("打印两个字符串") print('strA = ' + strA) print('strB = ' + strB) print("#############################") strC = 'I don\'t know anything' strD = '\'Yes\',I know.' print("字符串中的转义字符") print('strA = ' + strC) print('strB = ' + strD) print("#############################") strE = '这是我的blog,欢迎大家来\n我的博客溜达' print("字符串中的换行") print('strA = ' + strE) print("#############################") strF = 'this is ''message' strG = 'Hongten' strH = strG * 3 print('字符串可以用\'+\'号连接(或者说粘合),也可以用\'*\'号循环') print('strF原有形式为:\'this is \'\'message\'') print('粘合后的strF:' + strF) print('strG原值为:\'Hongten\',strH = strG * 3,此时strH为:' + strH) print("#############################") strI = 'hongtenzone@foxmail.com' print('字符串可以使用下标(索引)查询') print('源字符串strI = \'hongtenzone@foxmail.com\'') print('字符串strI的长度,len(strI) = ') print(len(strI)) print('strI[0] = ' + strI[0]) print('strI[10] = ' + strI[10]) print('strI[-1] = strI[len(strI) - 1]') print('strI[-1] = ' + strI[-1]) print('strI[len(strI) - 1] = ' + strI[len(strI) - 1]) print("#############################") print('Python 字符串不能改写。按字符串索引赋值会产生错误:') print('strI[0] = \'x\',这样就会产生错误啦') print("#############################") print('过大的索引代替为字符串大小,下界比上界大的返回空字符串') print('strI[0:100] = ' + strI[0:100]) print("#############################") print('索引可以是负数,计数从右边开始') print('strI[-2] = ' + strI[-2]) print('strI[-23:] = ' + strI[-23:]) print("#############################") print('不过-0 还是0,所以它不是从右边计数的!') print('strI[0] = ' + strI[0]) print('strI[-0] = ' + strI[-0])
运行效果如下:
Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> ================================ RESTART ================================ >>> 打印两个字符串 strA = this is a string strB = this is a message! ############################# 字符串中的转移字符 strA = I don't know anything strB = 'Yes',I know. ############################# 字符串中的换行 strA = 这是我的blog,欢迎大家来 我的博客溜达 ############################# 字符串可以用'+'号连接(或者说粘合),也可以用'*'号循环 strF原有形式为:'this is ''message' 粘合后的strF:this is message strG原值为:'Hongten',strH = strG * 3,此时strH为:HongtenHongtenHongten ############################# 字符串可以使用下标(索引)查询 源字符串strI = 'hongtenzone@foxmail.com' 字符串strI的长度,len(strI) = 23 strI[0] = h strI[10] = e strI[-1] = strI[len(strI) - 1] strI[-1] = m strI[len(strI) - 1] = m ############################# Python 字符串不能改写。按字符串索引赋值会产生错误: strI[0] = 'x',这样就会产生错误啦 ############################# 过大的索引代替为字符串大小,下界比上界大的返回空字符串 strI[0:100] = hongtenzone@foxmail.com ############################# 索引可以是负数,计数从右边开始 strI[-2] = o strI[-23:] = hongtenzone@foxmail.com ############################# 不过-0 还是0,所以它不是从右边计数的! strI[0] = h strI[-0] = h >>>
print打印字符串语句如下:
print('理解切片的最好方式是把索引视为两个字符之间的点,第一个字符的左边是0,字符串中第n个字符的右边是索引n') print(' +---+---+---+---+---+ ') print(' | H | e | l | p | A |') print(' +---+---+---+---+---+ ') print(' 0 1 2 3 4 5 ') print('-5 -4 -3 -2 -1 -0') print('第一行是字符串中给定的0到5各个索引的位置,第二行是对应的负索引。从i 到j 的切片由这两个标志之间的字符组成') print('对于非负索引,切片长度就是两索引的差。例如,word[1:3] 的长度是2')
运行效果如下:
Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> ================================ RESTART ================================ >>> 理解切片的最好方式是把索引视为两个字符之间的点,第一个字符的左边是0,字符串中第n个字符的右边是索引n +---+---+---+---+---+ | H | e | l | p | A | +---+---+---+---+---+ 0 1 2 3 4 5 -5 -4 -3 -2 -1 -0 第一行是字符串中给定的0到5各个索引的位置,第二行是对应的负索引。从i 到j 的切片由这两个标志之间的字符组成 对于非负索引,切片长度就是两索引的差。例如,word[1:3] 的长度是2 >>>
希望本文所述对大家Python程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.
