Change language

Python | Check if the line is repeated

|

Method # 1: Using a comprehension list + brute force
We can accomplish this task using selective slicing and brute force. This is a naive method for finding a string where we are trying to get the root string by splitting the string again.

# Python3 code to demonstrate how it works
# Check if the line is repeated
# Using brute force comprehension list

 
# initialization string

test_str = "GeeksforGeeksGeeksforGeeksGeeksforGeeks"

 
# print the original line

print ( "The original string is:" + test_str) < / code>

 
# using comprehension list + brute force
# Check if the line is repeated

res = None

for i in range ( 1 , len (test_str) / / 2 + 1 ):

if ( not len ( test_str) % len (test_str [ 0 : i]) and test_str [ 0 : i] *

  ( len (test_str) / / len (test_str [ 0 : i])) = = test_str):

  res = test_str [ 0 : i]

   # print result

print ( "The root substring of string:" + res)

Output:

 The original string is: GeeksforGeeksGeeksforGeeksPython.Engineering The root substring of string: Python.Engineering 

Method # 2: Using list slicing + find()

This problem can also be solved by the fact that we can search for the root string after adding the string and checking the root string in that string, except for the last and first character, which is a repeated string. 
Doesn’t work for line length "2.

# Python3 code to demonstrate how it works
# Check if the line is repeated
# Using list slicer + find ()

  
# initialization string

test_str = "GeeksforGeeksGeeksforGeeksGeeksforGeeks"

 
# print the original line

print ( "The original string is:" + test_str)

 
# using list slicing + find ()
# Check if the line is repeated

res = None

temp = (test_str + test_str) .find (test_str, 1 , - 1 )

if temp! = - 1 :

res = test_str [: temp]

  
# print result

print ( "The root substring of string:" + res)

Output:

 The original string is: GeeksforGeeksGeeksforGeeksPython.Engineering The root substring of string: Python.Engineering 

Python | Check if the line is repeated Python functions: Questions

Python | Check if the line is repeated repeat: Questions

Shop

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method