Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọche task is to write a Python program to increment the number which is at end of the string.
Bàn luận : test_str = ‘geeks006’
Output : geeks7
Explanation : Suffix 006 incremented to 7.Đưa ra một chuỗi, nhiệm vụ là viết một chương trình Python để tăng số ở cuối chuỗi. : test_str = ‘geeks007’
Output : geeks8
Explanation : Suffix 007 incremented to 8.
Đầu vào: test_str = ‘geek006, & nbsp; đầu ra: geek7 & nbsp; giải thích: hậu tố 006 tăng lên 7.join[] + replace[]
Đầu vào: test_str = ‘geek007, & nbsp; đầu ra: geek8 & nbsp; giải thích: hậu tố 007 tăng lên 8. & nbsp; & nbsp;
Python3
Phương thức số 1: Sử dụng findall [] + tham gia [] + thay thế []
Trong đó, chiến lược, chúng tôi thực hiện nhiệm vụ tìm số bằng cách sử dụng findall [], sau đó thực hiện nhiệm vụ phân tách chuỗi số và chuỗi tiền tố, sau đó thực hiện tăng chuỗi số được thực hiện. Cuối cùng, chuỗi được nối để có được một tiền tố theo sau là một số được tăng lên.
import
re
test_str
=
'geeks006'
print
____1010____11
The original string is : geeks006 Incremented numeric String : geeks0072
The original string is : geeks006 Incremented numeric String : geeks0073
The original string is : geeks006 Incremented numeric String : geeks0074
The original string is : geeks006 Incremented numeric String : geeks0075
=
The original string is : geeks006 Incremented numeric String : geeks0077
The original string is : geeks006 Incremented numeric String : geeks0078
The original string is : geeks006 Incremented numeric String : geeks0079
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4'] Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']0
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4'] Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']1
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4'] Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']2
=
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4'] Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']4
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4'] Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']5
=
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4'] Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']7
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4'] Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']8
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4'] Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']9
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4'] Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']0
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4'] Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']1
=
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4'] Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']3
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4'] Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']4
=
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4'] Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']6
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4'] Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']7
The original string is : geeks006 Incremented numeric String : geeks0072
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4'] Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']9
Output:
The original string is : geeks006 Incremented numeric String : geeks61
import
0=
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
1The original string is : geeks006
Incremented numeric String : geeks007
2 The original string is : geeks006
Incremented numeric String : geeks007
3import
5
print
The original string is : geeks006 Incremented numeric String : geeks0070
import
8 The original string is : geeks006 Incremented numeric String : geeks0072
The original string is : geeks006 Incremented numeric String : geeks0073
re
1Python3
Phương thức số 1: Sử dụng findall [] + tham gia [] + thay thế []
Trong đó, chiến lược, chúng tôi thực hiện nhiệm vụ tìm số bằng cách sử dụng findall [], sau đó thực hiện nhiệm vụ phân tách chuỗi số và chuỗi tiền tố, sau đó thực hiện tăng chuỗi số được thực hiện. Cuối cùng, chuỗi được nối để có được một tiền tố theo sau là một số được tăng lên.
import
re
test_str
=
'geeks006'
print
____1010____11
The original string is : geeks006 Incremented numeric String : geeks0072
The original string is : geeks006 Incremented numeric String : geeks0073
The original string is : geeks006 Incremented numeric String : geeks0074
test_str
8=
4
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4'] Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']4
=
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4'] Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']6
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4'] Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']7
The original string is : geeks006 Incremented numeric String : geeks0072
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4'] Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']9
Output:
The original string is : geeks006 Incremented numeric String : geeks007
import
0=
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4'] Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']1
The original string is : geeks006 Incremented numeric String : geeks0072
The original string is : geeks006 Incremented numeric String : geeks0073
import
5
The original string is : geeks006 Incremented numeric String : geeks0070import
8The original string is : geeks006 Incremented numeric String : geeks0072The original string is : geeks006 Incremented numeric String : geeks0073re
1O[n]Phương thức số 2: Sử dụng sub [] + nhóm [] + zfill []O[n]
Độ phức tạp về thời gian và không gian cho tất cả các phương pháp là như nhau:
Examples:
Độ phức tạp về thời gian: O [n] : test_list = [“gfg”, “234”, “is”, “98”, “123”, “best”, “4”], K = 6
Output : [‘gfg’, ‘240’, ‘is’, ‘104’, ‘129’, ‘best’, ’10’]
Explanation : 234, 98 and 4 are incremented by 6 in result.Độ phức tạp không gian: O [n] : test_list = [“gfg”, “234”, “is”, “98”, “123”, “best”, “4”], K = 4
Output : [‘gfg’, ‘238’, ‘is’, ‘102’, ‘129’, ‘best’, ‘8’]
Explanation : 234, 98 and 4 are incremented by 4 in result.
Phương pháp số 4: Sử dụng phương thức isNumeric [] str[] + int[] + loop + isdigit[]
import
0=
The original string is : geeks006 Incremented numeric String : geeks00709
Python3
'geeks006'
1=
'geeks006'
3'geeks006'
4'geeks006'
5'geeks006'
6'geeks006'
5'geeks006'
8'geeks006'
55____90'geeks006'
5print
2
print
____1010100
The original string is : geeks006 Incremented numeric String : geeks0072
The original string is : geeks006 Incremented numeric String : geeks0073
The original string is : geeks006 Incremented numeric String : geeks00703
The original string is : geeks006 Incremented numeric String : geeks00704
=
The original string is : geeks006 Incremented numeric String : geeks00706
import
0=
'geeks006'
3
The original string is : geeks006 Incremented numeric String : geeks0073__
print
The original string is : geeks006 Incremented numeric String : geeks0070
The original string is : geeks006 Incremented numeric String : geeks00732
The original string is : geeks006 Incremented numeric String : geeks0072
The original string is : geeks006 Incremented numeric String : geeks0073
re
1Danh sách ban đầu là: ['gfg', '234', 'là', '98', '123', 'tốt nhất', '4'] các chuỗi số tăng lên: ['gfg', '240', 'là' 'là' ' , '104', '129', 'tốt nhất', '10']]
Phương pháp số 3: Không sử dụng bất kỳ phương pháp tích hợp nào
The original string is : geeks006 Incremented numeric String : geeks00714
The original string is : geeks006 Incremented numeric String : geeks00726
The original string is : geeks006 Incremented numeric String : geeks00727
The original string is : geeks006 Incremented numeric String : geeks00717
The original string is : geeks006 Incremented numeric String : geeks00729
print
The original string is : geeks006 Incremented numeric String : geeks0070
The original string is : geeks006 Incremented numeric String : geeks00732
The original string is : geeks006 Incremented numeric String : geeks0072
The original string is : geeks006 Incremented numeric String : geeks0073
re
1Output:
Danh sách ban đầu là: ['gfg', '234', 'là', '98', '123', 'tốt nhất', '4'] các chuỗi số tăng lên: ['gfg', '240', 'là' 'là' ' , '104', '129', 'tốt nhất', '10']]
Phương pháp số 2: Sử dụng danh sách hiểu + isDigit []list comprehension + isdigit[]
Đây là một trong những cách mà nhiệm vụ này có thể được thực hiện. Tương tự như ở trên, chỉ một dòng thay thế để nén giải pháp bằng cách sử dụng danh sách hiểu.
Python3
'geeks006'
1=
'geeks006'
3'geeks006'
4'geeks006'
5'geeks006'
6'geeks006'
5'geeks006'
8'geeks006'
55____90'geeks006'
5print
2
print
____1010100
The original string is : geeks006 Incremented numeric String : geeks0072
The original string is : geeks006 Incremented numeric String : geeks0073
The original string is : geeks006 Incremented numeric String : geeks00703
The original string is : geeks006 Incremented numeric String : geeks00704
=
The original string is : geeks006 Incremented numeric String : geeks00706
import
0=
'geeks006'
3
The original string is : geeks006 Incremented numeric String : geeks0073__
print
The original string is : geeks006 Incremented numeric String : geeks0070
The original string is : geeks006 Incremented numeric String : geeks00732
The original string is : geeks006 Incremented numeric String : geeks0072
The original string is : geeks006 Incremented numeric String : geeks0073
re
1Output:
Danh sách ban đầu là: ['gfg', '234', 'là', '98', '123', 'tốt nhất', '4'] các chuỗi số tăng lên: ['gfg', '240', 'là' 'là' ' , '104', '129', 'tốt nhất', '10']]
Phương pháp số 3: Không sử dụng bất kỳ phương pháp tích hợp nào
Python3
The original string is : geeks006 Incremented numeric String : geeks00785
The original string is : geeks006 Incremented numeric String : geeks00786
The original string is : geeks006 Incremented numeric String : geeks00714
The original string is : geeks006 Incremented numeric String : geeks00788
=
The original string is : geeks006 Incremented numeric String : geeks00790
The original string is : geeks006 Incremented numeric String : geeks00714
The original string is : geeks006 Incremented numeric String : geeks00792
=
The original string is : geeks006 Incremented numeric String : geeks00794
The original string is : geeks006 Incremented numeric String : geeks00714
The original string is : geeks006 Incremented numeric String : geeks00710
The original string is : geeks006 Incremented numeric String : geeks00797
The original string is : geeks006 Incremented numeric String : geeks00712
The original string is : geeks006 Incremented numeric String : geeks00799
The original string is : geeks006 Incremented numeric String : geeks00717
The original string is : geeks006 Incremented numeric String : geeks00715
The original string is : geeks006 Incremented numeric String : geeks00797
The original string is : geeks006 Incremented numeric String : geeks00712
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4'] Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']04
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4'] Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']05
The original string is : geeks006 Incremented numeric String : geeks00792
The original string is : geeks006 Incremented numeric String : geeks0072
=
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4'] Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']9
The original string is : geeks006 Incremented numeric String : geeks00714
The original string is : geeks006 Incremented numeric String : geeks00715
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4'] Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']12
=
=
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4'] Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']15
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4'] Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']16
The original string is : geeks006 Incremented numeric String : geeks00717
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4'] Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']18
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4'] Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']19
The original string is : geeks006 Incremented numeric String : geeks00714
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4'] Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']18
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4'] Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']22
'geeks006'
1=
'geeks006'
3'geeks006'
4'geeks006'
5'geeks006'
6'geeks006'
5'geeks006'
8'geeks006'
55____90'geeks006'
5print
2
print
____1010100
The original string is : geeks006 Incremented numeric String : geeks0072
The original string is : geeks006 Incremented numeric String : geeks0073
The original string is : geeks006 Incremented numeric String : geeks00703
The original string is : geeks006 Incremented numeric String : geeks00704
=
The original string is : geeks006 Incremented numeric String : geeks00706
import
0=
'geeks006'
3
The original string is : geeks006 Incremented numeric String : geeks0073__
print
The original string is : geeks006 Incremented numeric String : geeks0070
The original string is : geeks006 Incremented numeric String : geeks00732
The original string is : geeks006 Incremented numeric String : geeks0072
The original string is : geeks006 Incremented numeric String : geeks0073
re
1The original string is : geeks006 Incremented numeric String : geeks00714
The original string is : geeks006 Incremented numeric String : geeks00715
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4'] Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']58
The original string is : geeks006 Incremented numeric String : geeks00717
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4'] Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']60
The original string is : geeks006 Incremented numeric String : geeks0073
The original string is : geeks006 Incremented numeric String : geeks0070
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4'] Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']6
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4'] Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']64
The original string is : geeks006 Incremented numeric String : geeks0072
The original string is : geeks006 Incremented numeric String : geeks00724
The original string is : geeks006 Incremented numeric String : geeks00714
The original string is : geeks006 Incremented numeric String : geeks00726
The original string is : geeks006 Incremented numeric String : geeks00727
The original string is : geeks006 Incremented numeric String : geeks00717
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4'] Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']71
Danh sách ban đầu là: ['gfg', '234', 'là', '98', '123', 'tốt nhất', '4'] các chuỗi số tăng lên: ['gfg', '240', 'là' 'là' ' , '104', '129', 'tốt nhất', '10']]
Phương pháp số 3: Không sử dụng bất kỳ phương pháp tích hợp nào
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4'] Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
The original string is : geeks006 Incremented numeric String : geeks00785
The original string is : geeks006 Incremented numeric String : geeks00786
The original string is : geeks006
Incremented numeric String : geeks007
14The original string is : geeks006
Incremented numeric String : geeks007
10 The original string is : geeks006
Incremented numeric String : geeks007
97The original string is : geeks006
Incremented numeric String : geeks007
12 The original string is : geeks006
Incremented numeric String : geeks007
99O[n]
The original string is : geeks006
Incremented numeric String : geeks007
17The original string is : geeks006
Incremented numeric String : geeks007
15 The original string is : geeks006
Incremented numeric String : geeks007
97The original string is : geeks006
Incremented numeric String : geeks007
12 The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
04O[n]
The original string is : geeks006
Incremented numeric String : geeks007
17The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
18 The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4']
Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']
19Using isnumeric[] method
Python3
'geeks006'
1=
'geeks006'
3'geeks006'
4'geeks006'
5'geeks006'
6'geeks006'
5'geeks006'
8'geeks006'
55____90'geeks006'
5print
2
print
____1010100
The original string is : geeks006 Incremented numeric String : geeks0072
The original string is : geeks006 Incremented numeric String : geeks0073
The original string is : geeks006 Incremented numeric String : geeks00703
The original string is : geeks006 Incremented numeric String : geeks00704
=
The original string is : geeks006 Incremented numeric String : geeks00706
import
0=
'geeks006'
3
The original string is : geeks006 Incremented numeric String : geeks0073__
print
The original string is : geeks006 Incremented numeric String : geeks0070
The original string is : geeks006 Incremented numeric String : geeks00732
The original string is : geeks006 Incremented numeric String : geeks0072
The original string is : geeks006 Incremented numeric String : geeks0073
re
1Danh sách ban đầu là: ['gfg', '234', 'là', '98', '123', 'tốt nhất', '4'] các chuỗi số tăng lên: ['gfg', '240', 'là' 'là' ' , '104', '129', 'tốt nhất', '10']]
Phương pháp số 3: Không sử dụng bất kỳ phương pháp tích hợp nào
The original string is : geeks006 Incremented numeric String : geeks00714
The original string is : geeks006 Incremented numeric String : geeks00726
The original string is : geeks006 Incremented numeric String : geeks00727
The original string is : geeks006 Incremented numeric String : geeks00717
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4'] Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']26
print
The original string is : geeks006 Incremented numeric String : geeks0070
The original string is : geeks006 Incremented numeric String : geeks00732
The original string is : geeks006 Incremented numeric String : geeks0072
The original string is : geeks006 Incremented numeric String : geeks0073
re
1Phương pháp số 3: Không sử dụng bất kỳ phương pháp tích hợp nào
The original list is : ['gfg', '234', 'is', '98', '123', 'best', '4'] Incremented Numeric Strings : ['gfg', '240', 'is', '104', '129', 'best', '10']