


An article to help you understand the basic string knowledge of Python
Why do we need strings?
When calling a browser to log in to some websites, you need to enter a password. After the browser sends the password to the server, the server will verify the password. The verification process is to compare the previous The saved password is compared with the password passed this time. If they are equal, the password is considered correct, otherwise it is considered incorrect; since the server wants to store these passwords, it can use a database (such as MySQL) to achieve this.
Of course, for the sake of simplicity, we can first find a variable to store the password; so how to store passwords with letters? This is where strings are used.
1. The format of strings in Python
is defined as follows Variable a stores a value of numeric type.
a = 100
The variable b defined below stores a string type value.
b = "hello itcast.cn" 或者 b = 'hello itcast.cn'
Small summary:
Double quotes or single The data in quotation marks is the string
二、字符串输出
例:
name = 'ming' position = '讲师' address = '中山市平区建材城西路金燕龙办公楼1层' print('--------------------------------------------------') print("姓名:%s"%name) print("职位:%s"%position) print("公司地址:%s"%address) print('--------------------------------------------------')
结果:
-------------------------------------------------- 姓名:ming 职位:讲师 公司地址:中山市昌平区建材城西路金燕龙办公楼1层 --------------------------------------------------
三、字符串输入
input通过它能够完成从键盘获取数据,然后保存到指定的变量中;
注意:input获取的数据,都以字符串的方式进行保存,即使输入的是数字,那么也是以字符串方式保存。
例:
userName = input('请输入用户名:') print("用户名为:%s"%userName) password = input('请输入密码:') print("密码为:%s"%password)
结果:(根据输入的不同结果也不同)
4. Subscripts and slicing
1. Subscript index
So-called"Subscript"
is the number, just like the number of the storage cabinet in the supermarket. You can find the corresponding storage space through this number.
"Subscript" in life
Supermarket locker
The use of "subscript" in strings
#Lists and tuples support subscript indexing for easy understanding. Strings are actually arrays of characters. So subscript indexing is also supported.
If there is a string: name = 'abcdef'
, the actual storage in memory is as follows:
如果想取出部分字符,那么可以通过下标
的方法,(注意Python中下标从 0 开始)
name = 'abcdef' print(name[0]) print(name[1]) print(name[2])
运行结果:
2. 切片的概念:
切片是指对操作的对象截取其中一部分的操作。字符串、列表、元组都支持切片操作。
3. 切片的语法:[起始:结束:步长]
注意:选取的区间属于左闭右开型,即从"起始"位开始,到"结束"位的前一位结束(不包含结束位本身)。
我们以字符串为例讲解。
如果取出一部分,则可以在中括号[]中,使用 :
例:
name = 'abcdef' print(name[0:3]) # 取 下标0~2 的字符
运行结果 :
例:
name = 'abcdef' print(name[0:5]) # 取 下标为0~4 的字符
运行结果:
例:
name = 'abcdef' print(name[3:5]) # 取 下标为3、4 的字符
运行结果:
例:
name = 'abcdef' print(name[2:]) # 取 下标为2开始到最后的字符
运行结果:
例:
name = 'abcdef' print(name[1:-1]) # 取 下标为1开始 到 最后第2个 之间的字符
运行结果:
>>> a = "abcdef" >>> a[:3] #运行结果 'abc' >>> a[::2] #运行结果 'ace' >>> a[5:1:2] '' #运行结果 >>> a[1:5:2] 'bd' #运行结果 >>> a[::-2] 'fdb' #运行结果 >>> a[5:1:-2] 'fd' #运行结果
5. Summary
This article explains the basics of Python (strings) in detail. Introduced operations on strings and slicing. Subscript index. and provide solutions to problems encountered in actual operations. I hope it can help you learn Python better.
The above is the detailed content of An article to help you understand the basic string knowledge of Python. For more information, please follow other related articles on the PHP Chinese website!

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 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.

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.

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 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.

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.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

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.

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.
