파이썬 | 문자열을 n개의 연속된 문자 그룹으로 분할

Python functions

예:

  입력:  str = "Geeksforgeeks", n = 3  출력:  [’Gee’ , ’ksf’,’ oor’, ’이런’,’ ks’]  입력:  str = "1234567891234567", n = 4  출력:  [1234, 5678, 9123 , 4567] 

방법 # 1: 목록 이해 사용

# 줄을 분할하는 Python 코드
# 세 번째 숫자마다


# 초기화 문자열

문자열 <코드 클래스 = "키워드"> = <코드 클래스 = "문자열"> "Geeksforgeeks"


# 분리점 정의

n = < 코드 클래스 = "값"> 3


<코드 클래스 = "comments"> # 이해 목록 사용

out = [(문자열 [i: i <코드 클래스 = "키워드"> + <코드 클래스 = "일반"> n]) <코드 클래스 = "키워드 "> i 범위 ( <코드 클래스 = "값"> 0 <코드 클래스 = "일반">, <코드 클래스 = "함수"> len <코드 class = "plain"> (문자열), n)]


# 출력물

< 코드 클래스 ="함수 "> 인쇄 <코드 클래스 = "일반"> (출력)

출력:

 [’Gee’,’ ksf’, ’org’,’ eek’, ’s’] 

방법 # 2: zip_longest

사용

# 숫자 문자열을 분할하는 Python 코드
# 및 4번째 숫자마다 기호


# 가져오기

itertools 가져오기 zip_longest


# zip_longest를 사용하여 분할하는 그룹 함수

def 그룹 (n, iterable, fillvalue = 없음 ):

<코드 클래스 = "일반"> 인수 <코드 클래스 = "키워드"> = <코드 클래스 = "일반"> [ iter (반복 가능)] * n

반환 zip_longest( 채우기 값 <코드 클래스 = "키워드"> = <코드 클래스 = "일반"> 채우기 값, <코드 클래스 = "키워드"> * <코드 클래스 = " plain"> 인수)


# 초기화 문자열

str <코드 클래스 = "키워드"> = <코드 클래스 = "문자열"> ’123GeeksForGeeks4567’


<코드 클래스 = "설명 "> # 분리점

n = 4


# 구분된 줄 목록

out_string = [’ ’ 그룹의 lis에 대한 .join(lis)(n , str, ’ <코드 클래스 = "일반"> ’) ]


# 출력 목록 초기화

out_no = []


# 문자열 목록을 목록으로 변환 정수의

for in out_string:

out_no.append (a)


# 목록 인쇄

인쇄 (out_no)

종료:

 [’123G’, ’익스’, ’ForG’,’익스’, ’4567’] 

Shop

Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$
Gifts for programmers

Best laptop for Zoom

$499
Gifts for programmers

Best laptop for Minecraft

$590

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically