日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python字符串 切片_用于切片字符串的Python程序

發(fā)布時間:2025/3/11 python 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python字符串 切片_用于切片字符串的Python程序 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

python字符串 切片

Given a string and number of characters (N), we have to slice and print the starting N characters from the given string using python program.

給定一個字符串和字符數(shù)( N ),我們必須使用python程序從給定的字符串中切片并打印開始的N個字符。

SLICING:

切片:

Slicing can be defined as selecting specific characters of a string. This can be performed easily by knowing the length of a string also if the length is not specified initially we can do it by selecting the number of characters that we require.

切片可以定義為選擇字符串的特定字符。 知道字符串的長度也可以很容易地做到這一點,如果最初沒有指定長度,我們可以通過選擇所需的字符數(shù)來實現(xiàn)。

Now let's understand slicing with a simple example,

現(xiàn)在讓我們用一個簡單的例子來了解切片

Question:

題:

We are provided with a string such that the first N characters are the front characters of the string. If the provided string length is less than N, the front is whatever we have got. Now your task is to create a new string such that it contains only the N characters from the front.

我們提供了一個字符串,使得前N個字符是字符串的開頭字符。 如果提供的字符串長度小于N ,則前面就是我們得到的。 現(xiàn)在,您的任務是創(chuàng)建一個新字符串,使其僅包含開頭的N個字符。

Example:

例:

slice('Javan', 3) = 'Jav'slice('Chocolava', 5) = 'Choco'slice('jio', 6) = 'jio'

Solution:

解:

Here we have considered N as a number of the front end. Here as the length of the string is not mentioned initially so here we have done slicing by considering a specific number of characters.

在這里,我們將N視為前端數(shù)。 在這里,因為最初沒有提到字符串的長度,所以在這里我們通過考慮特定數(shù)目的字符來進行切片。

Code:

碼:

def slice(str, n):if len(str) < n:n = len(str)front = str[:n]return frontprint (slice('Chocolate', 5)) print (slice('IncludeHelp', 7)) print (slice('Hello', 10)) #will print all characters

Output

輸出量

Choco Include Hello

翻譯自: https://www.includehelp.com/python/program-for-slicing-a-string.aspx

python字符串 切片

總結

以上是生活随笔為你收集整理的python字符串 切片_用于切片字符串的Python程序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。