Home Backend Development Python Tutorial python variable type - usage of python string str() (example)

python variable type - usage of python string str() (example)

Aug 13, 2018 pm 02:58 PM

In this article, I will talk about the python string str() in the standard numeric type in python. I hope it can help you who have just come into contact with python.

python string (String)

Python string is a string of characters composed of numbers, letters, and underscores. It can generally be recorded as follows:

1

s="a1a2···an"(n>=0)

Copy after login

(String is a data type that represents text in programming languages)

Python strings have two order of values:

1. From left to The right index starts from 0 by default, and the maximum range is 1 less than the string length.

2. The right-to-left index starts with -1 by default, and the maximum range is the beginning of the string.

If you want to take a section of string from a string, you can use [head subscript: tail subscript] to intercept the desired string, where the head subscript is calculated from 0. It can be a positive or negative number. If the tail subscript is empty, it means the head and tail are obtained. ([Head subscript: Tail subscript] The intercepted string includes the characters in the table below the head, but does not include the characters in the tail subscript. The character before the number written in the tail subscript is taken.) It is possible to just say it An unconvincing example is as follows:

1

2

3

4

5

#!/usr/bin/python

# -*- coding: UTF-8 -*-

 

s = '1458592158'

print s[2:6]

Copy after login

The output result of the above example is:

1

5859

Copy after login

When using colon-separated strings, python returns a new object, and the result contains the following For contiguous content marked by an offset, the left edge is inclusive of the lower border.

The above result contains the value b of s[1], and the maximum range obtained does not include the tail subscript, which is the value f of s[5].

python variable type - usage of python string str() (example)

The plus sign ( ) is the string concatenation operator, and the asterisk (*) is the repeating operation . The following example:

1

2

3

4

5

6

7

8

9

10

11

#!/usr/bin/python

# -*- coding: UTF-8 -*-

  

str = 'Hello World!'

  

print str           # 输出完整字符串

print str[0]        # 输出字符串中的第一个字符

print str[2:5]      # 输出字符串中第三个至第五个之间的字符串

print str[2:]       # 输出从第三个字符开始的字符串

print str * 2       # 输出字符串两次

print str + "TEST"  # 输出连接的字符串

Copy after login

The results of the above example are as follows:

1

2

3

4

5

6

Hello World!

H

llo

llo World!

Hello World!Hello World!

Hello World!TEST

Copy after login

The above content is some examples and applications of python strings. I hope to provide some help to friends who have just come into contact with python.

The above is the detailed content of python variable type - usage of python string str() (example). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How to teach computer novice programming basics in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How does Uvicorn continuously listen for HTTP requests without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

How to solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

How to get news data bypassing Investing.com's anti-crawler mechanism? How to get news data bypassing Investing.com's anti-crawler mechanism? Apr 02, 2025 am 07:03 AM

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...

See all articles