最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - How can I check whether a character is at least twice inside a string? - Stack Overflow

programmeradmin2浏览0评论

String string_data is read from input. If "r" is in string_data, then:

Output "At: " followed by the index of the first occurrence. Create a string from string_data with the first two occurrences of "r" replaced by "(deleted)" and output the new string on a new line.

Otherwise, output "No matches".

can anybody crack this one I know I may need an elif statement somewhere also need a else statement at the end for the otherwise part of the question.

I tried this code it works up until the else: statement or the otherwise, output "No Matches".

string_data = input()

if 'r' in string_data:
    index = string_data.find('r')    
    print(f'At:', index) 

new_str = string_data.replace("r", "(deleted)", 2) 
    
print(new_str, end='') 
print()

String string_data is read from input. If "r" is in string_data, then:

Output "At: " followed by the index of the first occurrence. Create a string from string_data with the first two occurrences of "r" replaced by "(deleted)" and output the new string on a new line.

Otherwise, output "No matches".

can anybody crack this one I know I may need an elif statement somewhere also need a else statement at the end for the otherwise part of the question.

I tried this code it works up until the else: statement or the otherwise, output "No Matches".

string_data = input()

if 'r' in string_data:
    index = string_data.find('r')    
    print(f'At:', index) 

new_str = string_data.replace("r", "(deleted)", 2) 
    
print(new_str, end='') 
print()
Share Improve this question edited 2 days ago Lajos Arpad 76.6k40 gold badges115 silver badges220 bronze badges asked 2 days ago Michael RomeroMichael Romero 11 silver badge4 bronze badges New contributor Michael Romero is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Add a comment  | 

4 Answers 4

Reset to default 2

The main problem with your code is that you only check whether r is in the string once instead of twice and you equate finding your first r inside the string with finding it twice.

Instead, you'll need to check whether it's inside your string at least twice. So you will need to replace your if with

if string_data.count("r") >= 2

and if that's true, then perform your replace on the first two instances. Then you'll need an else branch if this is not met. (I'm not very fluent in Python and have no means to test the code currently, so apologies in advance if I have a typo):

string_data = input()

print("\n")
if string_data.count("r") >= 2:
    print(string_data.replace("r", "(deleted)", 2))
else:
    print("No matches")

Your current replacement length trick is one way to go. Just to round it out, you could also use regular expressions here:

import re

string_data = input()
if re.search(r'r.*r', string_data):
    print("MATCH")
else:
    print("NO MATCH")

Here is the solution if you don't want to use replace() or count(). Just looping one time with existence counter


result, count = '', 0 

for char in string_data:
    if count >= 2 or char != 'r':
        result += char
        continue
        
    count += 1
    result += '(deleted)' if count <= 2 else char

if count >= 2:
    print(result)
else:
    print('No matches')

Print

abcde(deleted)fghijklmnopq(deleted)stuvrwxyzzzzzzz

This program takes a string as input and checks if the letter "r" is present. If it finds "r", it prints the position where it first appears. Then, it creates a new version of the string where the first two "r" occurrences are replaced with "(deleted)", and prints that updated string. If "r" is not found in the input, the program simply prints "No matches". This ensures that the program handles both cases—whether "r" is present or not.

string_data = input()
if "r" in string_data:
    first_index = string_data.index("r")
    print("At:", first_index)
    new_string = string_data.replace("r", "(deleted)", 2)
    print(new_string)

else:
    print("No matches")
发布评论

评论列表(0)

  1. 暂无评论